ClickHouse/src/Common/FiberStack.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

83 lines
2.9 KiB
C++
Raw Normal View History

2020-12-04 11:00:32 +00:00
#pragma once
2021-10-02 07:13:14 +00:00
#include <base/defines.h>
2020-12-02 11:18:46 +00:00
#include <boost/context/stack_context.hpp>
2020-12-15 09:22:14 +00:00
#include <Common/formatReadable.h>
#include <Common/CurrentMemoryTracker.h>
#include <Common/Exception.h>
2021-12-22 19:42:22 +00:00
#include <base/getPageSize.h>
2020-12-15 09:22:14 +00:00
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/mman.h>
2020-12-02 11:18:46 +00:00
#if defined(BOOST_USE_VALGRIND)
#include <valgrind/valgrind.h>
#endif
2020-12-15 09:22:14 +00:00
namespace DB::ErrorCodes
{
extern const int CANNOT_ALLOCATE_MEMORY;
}
2020-12-02 11:18:46 +00:00
/// This is an implementation of allocator for fiber stack.
2020-12-15 09:22:14 +00:00
/// 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.
2020-12-02 11:18:46 +00:00
class FiberStack
{
private:
size_t stack_size;
2020-12-15 09:22:14 +00:00
size_t page_size = 0;
2020-12-02 11:18:46 +00:00
public:
2021-09-07 12:56:32 +00:00
/// NOTE: If you see random segfaults in CI and stack starts from boost::context::...fiber...
/// probably it worth to try to increase stack size for coroutines.
///
/// Current value is just enough for all tests in our CI. It's not selected in some special
2022-03-24 14:50:32 +00:00
/// way. We will have 80 pages with 4KB page size.
static constexpr size_t default_stack_size = 320 * 1024; /// 64KB was not enough for tests
2020-12-02 11:18:46 +00:00
2020-12-15 09:22:14 +00:00
explicit FiberStack(size_t stack_size_ = default_stack_size) : stack_size(stack_size_)
{
2021-12-22 21:39:45 +00:00
page_size = getPageSize();
2020-12-15 09:22:14 +00:00
}
2020-12-02 11:18:46 +00:00
boost::context::stack_context allocate() const
2020-12-02 11:18:46 +00:00
{
2020-12-15 09:22:14 +00:00
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
2020-12-15 12:56:14 +00:00
void * vp = ::mmap(nullptr, num_bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
2020-12-15 09:22:14 +00:00
if (MAP_FAILED == vp)
DB::throwFromErrno(fmt::format("FiberStack: Cannot mmap {}.", ReadableSize(num_bytes)), DB::ErrorCodes::CANNOT_ALLOCATE_MEMORY);
2021-09-07 12:56:32 +00:00
/// TODO: make reports on illegal guard page access more clear.
/// Currently we will see segfault and almost random stacktrace.
2020-12-15 12:56:14 +00:00
if (-1 == ::mprotect(vp, page_size, PROT_NONE))
2020-12-15 09:22:14 +00:00
{
::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);
2020-12-02 11:18:46 +00:00
boost::context::stack_context sctx;
2020-12-15 09:22:14 +00:00
sctx.size = num_bytes;
2020-12-02 11:18:46 +00:00
sctx.sp = static_cast< char * >(vp) + sctx.size;
#if defined(BOOST_USE_VALGRIND)
sctx.valgrind_stack_id = VALGRIND_STACK_REGISTER(sctx.sp, vp);
#endif
return sctx;
}
void deallocate(boost::context::stack_context & sctx) const
2020-12-02 11:18:46 +00:00
{
#if defined(BOOST_USE_VALGRIND)
2020-12-07 18:19:22 +00:00
VALGRIND_STACK_DEREGISTER(sctx.valgrind_stack_id);
2020-12-02 11:18:46 +00:00
#endif
void * vp = static_cast< char * >(sctx.sp) - sctx.size;
2020-12-15 12:56:14 +00:00
::munmap(vp, sctx.size);
2020-12-15 09:22:14 +00:00
/// Do not count guard page in memory usage.
CurrentMemoryTracker::free(sctx.size - page_size);
2020-12-02 11:18:46 +00:00
}
};