2014-05-03 22:57:43 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
2016-07-31 03:53:16 +00:00
|
|
|
|
#include <atomic>
|
2015-09-29 19:19:54 +00:00
|
|
|
|
#include <common/Common.h>
|
2016-12-20 02:29:35 +00:00
|
|
|
|
#include <DB/Common/CurrentMetrics.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace CurrentMetrics
|
|
|
|
|
{
|
|
|
|
|
extern const Metric MemoryTracking;
|
|
|
|
|
}
|
2014-05-03 22:57:43 +00:00
|
|
|
|
|
|
|
|
|
|
2016-12-23 20:23:46 +00:00
|
|
|
|
/** Tracks memory consumption.
|
|
|
|
|
* It throws an exception if amount of consumed memory become greater than certain limit.
|
|
|
|
|
* The same memory tracker could be simultaneously used in different threads.
|
2014-05-03 22:57:43 +00:00
|
|
|
|
*/
|
|
|
|
|
class MemoryTracker
|
|
|
|
|
{
|
2016-07-31 03:53:16 +00:00
|
|
|
|
std::atomic<Int64> amount {0};
|
|
|
|
|
std::atomic<Int64> peak {0};
|
2017-02-01 03:53:10 +00:00
|
|
|
|
std::atomic<Int64> limit {0};
|
2014-05-03 22:57:43 +00:00
|
|
|
|
|
2016-12-23 20:23:46 +00:00
|
|
|
|
/// To test exception safety of calling code, memory tracker throws an exception on each memory allocation with specified probability.
|
2015-12-23 07:39:28 +00:00
|
|
|
|
double fault_probability = 0;
|
|
|
|
|
|
2016-12-23 20:23:46 +00:00
|
|
|
|
/// Singly-linked list. All information will be passed to subsequent memory trackers also (it allows to implement trackers hierarchy).
|
|
|
|
|
/// In terms of tree nodes it is the list of parents. Lifetime of these trackers should "include" lifetime of current tracker.
|
2015-12-30 15:39:11 +00:00
|
|
|
|
MemoryTracker * next = nullptr;
|
|
|
|
|
|
2016-12-20 02:29:35 +00:00
|
|
|
|
/// You could specify custom metric to track memory usage.
|
|
|
|
|
CurrentMetrics::Metric metric = CurrentMetrics::MemoryTracking;
|
|
|
|
|
|
2016-12-23 20:23:46 +00:00
|
|
|
|
/// This description will be used as prefix into log messages (if isn't nullptr)
|
2015-12-30 15:39:11 +00:00
|
|
|
|
const char * description = nullptr;
|
|
|
|
|
|
2014-05-03 22:57:43 +00:00
|
|
|
|
public:
|
2016-01-13 02:38:30 +00:00
|
|
|
|
MemoryTracker() {}
|
2014-05-08 15:54:51 +00:00
|
|
|
|
MemoryTracker(Int64 limit_) : limit(limit_) {}
|
2014-05-03 22:57:43 +00:00
|
|
|
|
|
|
|
|
|
~MemoryTracker();
|
|
|
|
|
|
2016-12-23 20:23:46 +00:00
|
|
|
|
/** Call the following functions before calling of corresponding operations with memory allocators.
|
2014-05-03 22:57:43 +00:00
|
|
|
|
*/
|
2014-05-08 15:54:51 +00:00
|
|
|
|
void alloc(Int64 size);
|
2014-05-03 22:57:43 +00:00
|
|
|
|
|
2014-05-08 15:54:51 +00:00
|
|
|
|
void realloc(Int64 old_size, Int64 new_size)
|
2014-05-03 22:57:43 +00:00
|
|
|
|
{
|
|
|
|
|
alloc(new_size - old_size);
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-23 20:23:46 +00:00
|
|
|
|
/** This function should be called after memory deallocation.
|
2014-05-03 22:57:43 +00:00
|
|
|
|
*/
|
2015-12-30 15:39:11 +00:00
|
|
|
|
void free(Int64 size);
|
2014-05-03 22:57:43 +00:00
|
|
|
|
|
2014-05-08 15:54:51 +00:00
|
|
|
|
Int64 get() const
|
2014-05-03 22:57:43 +00:00
|
|
|
|
{
|
2016-07-31 03:53:16 +00:00
|
|
|
|
return amount.load(std::memory_order_relaxed);
|
2014-05-03 22:57:43 +00:00
|
|
|
|
}
|
2015-07-01 05:31:27 +00:00
|
|
|
|
|
|
|
|
|
Int64 getPeak() const
|
|
|
|
|
{
|
2016-07-31 03:53:16 +00:00
|
|
|
|
return peak.load(std::memory_order_relaxed);
|
2015-07-01 05:31:27 +00:00
|
|
|
|
}
|
2015-12-23 07:39:28 +00:00
|
|
|
|
|
2016-01-13 02:38:30 +00:00
|
|
|
|
void setLimit(Int64 limit_)
|
|
|
|
|
{
|
2017-02-01 03:53:10 +00:00
|
|
|
|
limit.store(limit_, std::memory_order_relaxed);
|
2016-01-13 02:38:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-01 03:53:10 +00:00
|
|
|
|
/** Set limit if it was not set.
|
|
|
|
|
* Otherwise, set limit to new value, if new value is greater than previous limit.
|
|
|
|
|
*/
|
|
|
|
|
void setOrRaiseLimit(Int64 value);
|
|
|
|
|
|
2015-12-23 07:39:28 +00:00
|
|
|
|
void setFaultProbability(double value)
|
|
|
|
|
{
|
|
|
|
|
fault_probability = value;
|
|
|
|
|
}
|
2015-12-30 15:39:11 +00:00
|
|
|
|
|
2016-01-13 03:59:24 +00:00
|
|
|
|
void setNext(MemoryTracker * elem)
|
2015-12-30 15:39:11 +00:00
|
|
|
|
{
|
2016-01-13 03:59:24 +00:00
|
|
|
|
next = elem;
|
2015-12-30 15:39:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-23 20:23:46 +00:00
|
|
|
|
/// The memory consumption could be shown in realtime via CurrentMetrics counter
|
2016-12-20 02:29:35 +00:00
|
|
|
|
void setMetric(CurrentMetrics::Metric metric_)
|
|
|
|
|
{
|
|
|
|
|
metric = metric_;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-30 15:39:11 +00:00
|
|
|
|
void setDescription(const char * description_)
|
|
|
|
|
{
|
|
|
|
|
description = description_;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-23 20:23:46 +00:00
|
|
|
|
/// Reset the accumulated data.
|
2015-12-30 15:39:11 +00:00
|
|
|
|
void reset();
|
|
|
|
|
|
2016-12-23 20:23:46 +00:00
|
|
|
|
/// Prints info about peak memory consumption into log.
|
2015-12-30 15:39:11 +00:00
|
|
|
|
void logPeakMemoryUsage() const;
|
2014-05-03 22:57:43 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Объект MemoryTracker довольно трудно протащить во все места, где выделяются существенные объёмы памяти.
|
|
|
|
|
* Поэтому, используется thread-local указатель на используемый MemoryTracker или nullptr, если его не нужно использовать.
|
|
|
|
|
* Этот указатель выставляется, когда в данном потоке следует отслеживать потребление памяти.
|
|
|
|
|
* Таким образом, его нужно всего-лишь протащить во все потоки, в которых обрабатывается один запрос.
|
|
|
|
|
*/
|
|
|
|
|
extern __thread MemoryTracker * current_memory_tracker;
|
2016-10-25 14:22:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
|
|
|
|
|
|
struct TemporarilyDisableMemoryTracker : private boost::noncopyable
|
|
|
|
|
{
|
|
|
|
|
MemoryTracker * memory_tracker;
|
|
|
|
|
|
|
|
|
|
TemporarilyDisableMemoryTracker()
|
|
|
|
|
{
|
|
|
|
|
memory_tracker = current_memory_tracker;
|
|
|
|
|
current_memory_tracker = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~TemporarilyDisableMemoryTracker()
|
|
|
|
|
{
|
|
|
|
|
current_memory_tracker = memory_tracker;
|
|
|
|
|
}
|
|
|
|
|
};
|