2015-09-29 19:19:54 +00:00
|
|
|
#include <common/likely.h>
|
|
|
|
#include <common/logger_useful.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <Common/formatReadable.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
2014-05-08 12:00:47 +00:00
|
|
|
#include <iomanip>
|
2014-05-03 22:57:43 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/MemoryTracker.h>
|
2014-05-03 22:57:43 +00:00
|
|
|
|
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int MEMORY_LIMIT_EXCEEDED;
|
|
|
|
}
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-03 22:57:43 +00:00
|
|
|
MemoryTracker::~MemoryTracker()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (peak)
|
2017-10-27 17:24:33 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
logPeakMemoryUsage();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
/// Exception in Logger, intentionally swallow.
|
|
|
|
}
|
|
|
|
}
|
2016-01-21 01:47:28 +00:00
|
|
|
|
2017-04-11 19:10:31 +00:00
|
|
|
/** This is needed for next memory tracker to be consistent with sum of all referring memory trackers.
|
|
|
|
*
|
|
|
|
* Sometimes, memory tracker could be destroyed before memory was freed, and on destruction, amount > 0.
|
|
|
|
* For example, a query could allocate some data and leave it in cache.
|
|
|
|
*
|
|
|
|
* If memory will be freed outside of context of this memory tracker,
|
|
|
|
* but in context of one of the 'next' memory trackers,
|
|
|
|
* then memory usage of 'next' memory trackers will be underestimated,
|
|
|
|
* because amount will be decreased twice (first - here, second - when real 'free' happens).
|
|
|
|
*/
|
2017-09-17 00:13:26 +00:00
|
|
|
if (auto value = amount.load(std::memory_order_relaxed))
|
|
|
|
free(value);
|
2014-05-03 22:57:43 +00:00
|
|
|
}
|
|
|
|
|
2015-12-30 15:39:11 +00:00
|
|
|
|
|
|
|
void MemoryTracker::logPeakMemoryUsage() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
LOG_DEBUG(&Logger::get("MemoryTracker"),
|
|
|
|
"Peak memory usage" << (description ? " " + std::string(description) : "")
|
|
|
|
<< ": " << formatReadableSizeWithBinarySuffix(peak) << ".");
|
2015-12-30 15:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-08 15:54:51 +00:00
|
|
|
void MemoryTracker::alloc(Int64 size)
|
2014-05-03 22:57:43 +00:00
|
|
|
{
|
2017-09-17 00:13:26 +00:00
|
|
|
/** Using memory_order_relaxed means that if allocations are done simultaneously,
|
|
|
|
* we allow exception about memory limit exceeded to be thrown only on next allocation.
|
|
|
|
* So, we allow over-allocations.
|
|
|
|
*/
|
|
|
|
Int64 will_be = size + amount.fetch_add(size, std::memory_order_relaxed);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-09-09 04:06:54 +00:00
|
|
|
if (!next.load(std::memory_order_relaxed))
|
2017-04-01 07:20:54 +00:00
|
|
|
CurrentMetrics::add(metric, size);
|
|
|
|
|
|
|
|
Int64 current_limit = limit.load(std::memory_order_relaxed);
|
|
|
|
|
|
|
|
/// Using non-thread-safe random number generator. Joint distribution in different threads would not be uniform.
|
|
|
|
/// In this case, it doesn't matter.
|
|
|
|
if (unlikely(fault_probability && drand48() < fault_probability))
|
|
|
|
{
|
|
|
|
free(size);
|
|
|
|
|
|
|
|
std::stringstream message;
|
|
|
|
message << "Memory tracker";
|
|
|
|
if (description)
|
|
|
|
message << " " << description;
|
|
|
|
message << ": fault injected. Would use " << formatReadableSizeWithBinarySuffix(will_be)
|
|
|
|
<< " (attempt to allocate chunk of " << size << " bytes)"
|
|
|
|
<< ", maximum: " << formatReadableSizeWithBinarySuffix(current_limit);
|
|
|
|
|
|
|
|
throw DB::Exception(message.str(), DB::ErrorCodes::MEMORY_LIMIT_EXCEEDED);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unlikely(current_limit && will_be > current_limit))
|
|
|
|
{
|
|
|
|
free(size);
|
|
|
|
|
|
|
|
std::stringstream message;
|
|
|
|
message << "Memory limit";
|
|
|
|
if (description)
|
|
|
|
message << " " << description;
|
|
|
|
message << " exceeded: would use " << formatReadableSizeWithBinarySuffix(will_be)
|
|
|
|
<< " (attempt to allocate chunk of " << size << " bytes)"
|
|
|
|
<< ", maximum: " << formatReadableSizeWithBinarySuffix(current_limit);
|
|
|
|
|
|
|
|
throw DB::Exception(message.str(), DB::ErrorCodes::MEMORY_LIMIT_EXCEEDED);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (will_be > peak.load(std::memory_order_relaxed)) /// Races doesn't matter. Could rewrite with CAS, but not worth.
|
|
|
|
peak.store(will_be, std::memory_order_relaxed);
|
|
|
|
|
2017-09-09 04:06:54 +00:00
|
|
|
if (auto loaded_next = next.load(std::memory_order_relaxed))
|
|
|
|
loaded_next->alloc(size);
|
2015-12-30 15:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MemoryTracker::free(Int64 size)
|
|
|
|
{
|
2017-09-17 00:13:26 +00:00
|
|
|
Int64 new_amount = amount.fetch_sub(size, std::memory_order_relaxed) - size;
|
|
|
|
|
2017-04-11 19:10:31 +00:00
|
|
|
/** Sometimes, query could free some data, that was allocated outside of query context.
|
|
|
|
* Example: cache eviction.
|
|
|
|
* To avoid negative memory usage, we "saturate" amount.
|
|
|
|
* Memory usage will be calculated with some error.
|
2017-09-17 00:13:26 +00:00
|
|
|
* NOTE The code is not atomic. Not worth to fix.
|
2017-04-11 19:10:31 +00:00
|
|
|
*/
|
2017-09-17 00:13:26 +00:00
|
|
|
if (new_amount < 0)
|
|
|
|
{
|
|
|
|
amount.fetch_sub(new_amount);
|
|
|
|
size += new_amount;
|
|
|
|
}
|
2015-12-30 15:39:11 +00:00
|
|
|
|
2017-09-09 04:06:54 +00:00
|
|
|
if (auto loaded_next = next.load(std::memory_order_relaxed))
|
|
|
|
loaded_next->free(size);
|
2017-04-01 07:20:54 +00:00
|
|
|
else
|
2017-09-17 00:13:26 +00:00
|
|
|
CurrentMetrics::sub(metric, size);
|
2015-12-30 15:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MemoryTracker::reset()
|
|
|
|
{
|
2017-09-09 04:06:54 +00:00
|
|
|
if (!next.load(std::memory_order_relaxed))
|
2017-09-17 00:13:26 +00:00
|
|
|
CurrentMetrics::sub(metric, amount.load(std::memory_order_relaxed));
|
2016-01-21 01:47:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
amount.store(0, std::memory_order_relaxed);
|
|
|
|
peak.store(0, std::memory_order_relaxed);
|
|
|
|
limit.store(0, std::memory_order_relaxed);
|
2017-02-01 03:53:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MemoryTracker::setOrRaiseLimit(Int64 value)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/// This is just atomic set to maximum.
|
|
|
|
Int64 old_value = limit.load(std::memory_order_relaxed);
|
|
|
|
while (old_value < value && !limit.compare_exchange_weak(old_value, value))
|
|
|
|
;
|
2014-05-03 22:57:43 +00:00
|
|
|
}
|
|
|
|
|
2017-12-16 01:38:37 +00:00
|
|
|
#if __APPLE__ && __clang__
|
2017-11-15 20:28:34 +00:00
|
|
|
__thread MemoryTracker * current_memory_tracker = nullptr;
|
|
|
|
#else
|
2017-10-19 19:36:37 +00:00
|
|
|
thread_local MemoryTracker * current_memory_tracker = nullptr;
|
2017-11-15 20:28:34 +00:00
|
|
|
#endif
|
2017-04-10 21:40:38 +00:00
|
|
|
|
|
|
|
namespace CurrentMemoryTracker
|
|
|
|
{
|
|
|
|
void alloc(Int64 size)
|
|
|
|
{
|
|
|
|
if (current_memory_tracker)
|
|
|
|
current_memory_tracker->alloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void realloc(Int64 old_size, Int64 new_size)
|
|
|
|
{
|
|
|
|
if (current_memory_tracker)
|
|
|
|
current_memory_tracker->alloc(new_size - old_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void free(Int64 size)
|
|
|
|
{
|
|
|
|
if (current_memory_tracker)
|
|
|
|
current_memory_tracker->free(size);
|
|
|
|
}
|
|
|
|
}
|