2016-01-20 21:32:01 +00:00
|
|
|
#pragma once
|
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
#include <stddef.h>
|
2016-01-21 01:47:28 +00:00
|
|
|
#include <cstdint>
|
2016-01-20 21:32:01 +00:00
|
|
|
#include <utility>
|
2016-07-31 03:53:16 +00:00
|
|
|
#include <atomic>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Core/Types.h>
|
2016-01-20 21:32:01 +00:00
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
/** Allows to count number of simultaneously happening processes or current value of some metric.
|
|
|
|
* - for high-level profiling.
|
2016-01-20 21:32:01 +00:00
|
|
|
*
|
2016-10-24 04:06:27 +00:00
|
|
|
* See also ProfileEvents.h
|
|
|
|
* ProfileEvents counts number of happened events - for example, how many times queries was executed.
|
|
|
|
* CurrentMetrics counts number of simultaneously happening events - for example, number of currently executing queries, right now,
|
|
|
|
* or just current value of some metric - for example, replica delay in seconds.
|
|
|
|
*
|
|
|
|
* CurrentMetrics are updated instantly and are correct for any point in time.
|
|
|
|
* For periodically (asynchronously) updated metrics, see AsynchronousMetrics.h
|
2016-01-20 21:32:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace CurrentMetrics
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Metric identifier (index in array).
|
|
|
|
using Metric = size_t;
|
|
|
|
using Value = DB::Int64;
|
|
|
|
|
2018-09-01 19:51:38 +00:00
|
|
|
/// Get name of metric by identifier. Returns statically allocated string.
|
|
|
|
const char * getName(Metric event);
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Get text description of metric by identifier. Returns statically allocated string.
|
2018-09-01 19:51:38 +00:00
|
|
|
const char * getDocumentation(Metric event);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/// Metric identifier -> current value of metric.
|
|
|
|
extern std::atomic<Value> values[];
|
|
|
|
|
|
|
|
/// Get index just after last metric identifier.
|
|
|
|
Metric end();
|
|
|
|
|
|
|
|
/// Set value of specified metric.
|
|
|
|
inline void set(Metric metric, Value value)
|
|
|
|
{
|
2017-09-17 00:13:26 +00:00
|
|
|
values[metric].store(value, std::memory_order_relaxed);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Add value for specified metric. You must subtract value later; or see class Increment below.
|
|
|
|
inline void add(Metric metric, Value value = 1)
|
|
|
|
{
|
2017-09-17 00:13:26 +00:00
|
|
|
values[metric].fetch_add(value, std::memory_order_relaxed);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void sub(Metric metric, Value value = 1)
|
|
|
|
{
|
|
|
|
add(metric, -value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// For lifetime of object, add amout for specified metric. Then subtract.
|
|
|
|
class Increment
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::atomic<Value> * what;
|
|
|
|
Value amount;
|
|
|
|
|
2019-08-03 11:02:40 +00:00
|
|
|
Increment(std::atomic<Value> * what_, Value amount_)
|
|
|
|
: what(what_), amount(amount_)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
*what += amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2019-08-03 11:02:40 +00:00
|
|
|
Increment(Metric metric, Value amount_ = 1)
|
|
|
|
: Increment(&values[metric], amount_) {}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
~Increment()
|
|
|
|
{
|
|
|
|
if (what)
|
2017-09-17 00:13:26 +00:00
|
|
|
what->fetch_sub(amount, std::memory_order_relaxed);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Increment(Increment && old)
|
|
|
|
{
|
|
|
|
*this = std::move(old);
|
|
|
|
}
|
|
|
|
|
|
|
|
Increment & operator= (Increment && old)
|
|
|
|
{
|
|
|
|
what = old.what;
|
|
|
|
amount = old.amount;
|
|
|
|
old.what = nullptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void changeTo(Value new_amount)
|
|
|
|
{
|
2017-09-17 00:13:26 +00:00
|
|
|
what->fetch_add(new_amount - amount, std::memory_order_relaxed);
|
2017-04-01 07:20:54 +00:00
|
|
|
amount = new_amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Subtract value before destructor.
|
|
|
|
void destroy()
|
|
|
|
{
|
2017-09-17 00:13:26 +00:00
|
|
|
what->fetch_sub(amount, std::memory_order_relaxed);
|
2017-04-01 07:20:54 +00:00
|
|
|
what = nullptr;
|
|
|
|
}
|
|
|
|
};
|
2016-01-20 21:32:01 +00:00
|
|
|
}
|