ClickHouse/dbms/include/DB/Common/CurrentMetrics.h

103 lines
2.3 KiB
C++
Raw Normal View History

#pragma once
#include <stddef.h>
#include <cstdint>
#include <utility>
2016-07-31 03:53:16 +00:00
#include <atomic>
#include <DB/Core/Types.h>
/** Allows to count number of simultaneously happening processes or current value of some metric.
* - for high-level profiling.
*
* 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
*/
namespace CurrentMetrics
{
/// Metric identifier (index in array).
using Metric = size_t;
using Value = DB::Int64;
/// Get text description of metric by identifier. Returns statically allocated string.
const char * getDescription(Metric event);
/// 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)
{
values[metric] = value;
}
/// Add value for specified metric. You must subtract value later; or see class Increment below.
inline void add(Metric metric, Value value = 1)
{
2016-07-31 03:53:16 +00:00
values[metric] += value;
}
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:
2016-07-31 03:53:16 +00:00
std::atomic<Value> * what;
Value amount;
2016-07-31 03:53:16 +00:00
Increment(std::atomic<Value> * what, Value amount)
: what(what), amount(amount)
{
2016-07-31 03:53:16 +00:00
*what += amount;
}
public:
Increment(Metric metric, Value amount = 1)
: Increment(&values[metric], amount) {}
~Increment()
{
if (what)
2016-07-31 03:53:16 +00:00
*what -= amount;
}
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)
{
2016-07-31 03:53:16 +00:00
*what += new_amount - amount;
amount = new_amount;
}
/// Subtract value before destructor.
void destroy()
{
2016-07-31 03:53:16 +00:00
*what -= amount;
what = nullptr;
}
};
}