2015-09-29 19:19:54 +00:00
|
|
|
#include <common/likely.h>
|
|
|
|
#include <common/logger_useful.h>
|
2014-05-03 22:57:43 +00:00
|
|
|
#include <DB/Core/Exception.h>
|
2014-10-25 18:33:52 +00:00
|
|
|
#include <DB/Common/formatReadable.h>
|
2014-05-03 22:57:43 +00:00
|
|
|
#include <DB/IO/WriteHelpers.h>
|
2014-05-08 12:00:47 +00:00
|
|
|
#include <iomanip>
|
2014-05-03 22:57:43 +00:00
|
|
|
|
|
|
|
#include <DB/Common/MemoryTracker.h>
|
|
|
|
|
|
|
|
|
|
|
|
MemoryTracker::~MemoryTracker()
|
|
|
|
{
|
2014-10-25 18:33:52 +00:00
|
|
|
LOG_DEBUG(&Logger::get("MemoryTracker"), "Peak memory usage for query: " << formatReadableSizeWithBinarySuffix(peak) << ".");
|
2014-05-03 22:57:43 +00:00
|
|
|
}
|
|
|
|
|
2014-05-08 15:54:51 +00:00
|
|
|
void MemoryTracker::alloc(Int64 size)
|
2014-05-03 22:57:43 +00:00
|
|
|
{
|
2014-05-08 15:54:51 +00:00
|
|
|
Int64 will_be = __sync_add_and_fetch(&amount, size);
|
2014-05-03 22:57:43 +00:00
|
|
|
|
|
|
|
if (unlikely(limit && will_be > limit))
|
|
|
|
{
|
|
|
|
free(size);
|
2014-10-25 18:33:52 +00:00
|
|
|
throw DB::Exception("Memory limit exceeded: would use " + formatReadableSizeWithBinarySuffix(will_be) + ""
|
2014-05-03 22:57:43 +00:00
|
|
|
" (attempt to allocate chunk of " + DB::toString(size) + " bytes)"
|
2014-10-25 18:33:52 +00:00
|
|
|
", maximum: " + formatReadableSizeWithBinarySuffix(limit), DB::ErrorCodes::MEMORY_LIMIT_EXCEEDED);
|
2014-05-03 22:57:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (will_be > peak)
|
|
|
|
peak = will_be;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
__thread MemoryTracker * current_memory_tracker = nullptr;
|