Update FiberStack

This commit is contained in:
Nikolai Kochetov 2020-12-15 12:22:14 +03:00
parent c4e80ccfc5
commit d070920c72

View File

@ -1,34 +1,57 @@
#pragma once
#include <common/defines.h>
#include <boost/context/stack_context.hpp>
#include <Common/Allocator.h>
#include <Common/formatReadable.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/mman.h>
#if defined(BOOST_USE_VALGRIND)
#include <valgrind/valgrind.h>
#endif
namespace DB::ErrorCodes
{
extern const int CANNOT_ALLOCATE_MEMORY;
}
/// This is an implementation of allocator for fiber stack.
/// It uses internal allocator, so we track memory usage. It is the main reason why this class is needed.
/// The reference implementations are pooled_fixedsize_stack and protected_fixedsize_stack from boost::context.
template <typename TAllocator = Allocator<false, false>>
/// The reference implementation is protected_fixedsize_stack from boost::context.
/// This implementation additionally track memory usage. It is the main reason why it is needed.
class FiberStack
{
private:
size_t stack_size;
size_t page_size = 0;
public:
/// 8MB of memory per fiber stack may seem too expensive. It is indeed.
/// The reason is that current (patched) libunwind needs > 4MB of stack memory to unwind stack.
/// If we allocate less memory, any thrown exception inside fiber will cause segfault.
static constexpr size_t default_stack_size = 8 * 1024 * 1024;
static constexpr size_t default_stack_size = 64 * 1024;
explicit FiberStack(size_t stack_size_ = default_stack_size) : stack_size(stack_size_) {}
explicit FiberStack(size_t stack_size_ = default_stack_size) : stack_size(stack_size_)
{
page_size = ::sysconf( _SC_PAGESIZE);
}
boost::context::stack_context allocate()
{
void * vp = TAllocator().alloc(stack_size);
size_t num_pages = 1 + (stack_size - 1) / page_size;
size_t num_bytes = (num_pages + 1) * page_size; /// Add one page at bottom that will be used as guard-page
void * vp = ::mmap( nullptr, num_bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (MAP_FAILED == vp)
DB::throwFromErrno(fmt::format("FiberStack: Cannot mmap {}.", ReadableSize(num_bytes)), DB::ErrorCodes::CANNOT_ALLOCATE_MEMORY);
if (-1 == ::mprotect( vp, page_size, PROT_NONE))
{
::munmap(vp, num_bytes);
DB::throwFromErrno("FiberStack: cannot protect guard page", DB::ErrorCodes::CANNOT_ALLOCATE_MEMORY);
}
/// Do not count guard page in memory usage.
CurrentMemoryTracker::alloc(num_pages * page_size);
boost::context::stack_context sctx;
sctx.size = stack_size;
sctx.size = num_bytes;
sctx.sp = static_cast< char * >(vp) + sctx.size;
#if defined(BOOST_USE_VALGRIND)
sctx.valgrind_stack_id = VALGRIND_STACK_REGISTER(sctx.sp, vp);
@ -42,6 +65,9 @@ public:
VALGRIND_STACK_DEREGISTER(sctx.valgrind_stack_id);
#endif
void * vp = static_cast< char * >(sctx.sp) - sctx.size;
TAllocator().free(vp, stack_size);
::munmap( vp, sctx.size);
/// Do not count guard page in memory usage.
CurrentMemoryTracker::free(sctx.size - page_size);
}
};