aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/coroutine/engine/stack/stack_utils.cpp
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/coroutine/engine/stack/stack_utils.cpp
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/coroutine/engine/stack/stack_utils.cpp')
-rw-r--r--library/cpp/coroutine/engine/stack/stack_utils.cpp84
1 files changed, 84 insertions, 0 deletions
diff --git a/library/cpp/coroutine/engine/stack/stack_utils.cpp b/library/cpp/coroutine/engine/stack/stack_utils.cpp
new file mode 100644
index 0000000000..1548529b66
--- /dev/null
+++ b/library/cpp/coroutine/engine/stack/stack_utils.cpp
@@ -0,0 +1,84 @@
+#include "stack_utils.h"
+
+#include <contrib/libs/linux-headers/asm-generic/errno-base.h>
+#include <util/generic/scope.h>
+#include <util/system/yassert.h>
+
+#ifdef _linux_
+#include <sys/mman.h>
+#endif
+
+#include <cerrno>
+#include <cstdlib>
+#include <cstring>
+
+
+namespace NCoro::NStack {
+
+#ifdef _linux_
+ bool GetAlignedMemory(uint64_t sizeInPages, char*& rawPtr, char*& alignedPtr) noexcept {
+ Y_ASSERT(sizeInPages);
+
+ void* ptr = nullptr;
+ int error = posix_memalign(&ptr, PageSize, sizeInPages * PageSize);
+ alignedPtr = rawPtr = (char*)ptr;
+ return rawPtr && alignedPtr && !error;
+ }
+#else
+ bool GetAlignedMemory(uint64_t sizeInPages, char*& rawPtr, char*& alignedPtr) noexcept {
+ Y_ASSERT(sizeInPages);
+
+ rawPtr = (char*) malloc((sizeInPages + 1) * PageSize); // +1 in case result would be unaligned
+ alignedPtr = (char*)( ((uint64_t)rawPtr + PageSize - 1) & ~PageSizeMask);
+ return rawPtr && alignedPtr;
+ }
+#endif
+
+#ifdef _linux_
+ void ReleaseRss(char* alignedPtr, uint64_t numOfPages) noexcept {
+ Y_VERIFY( !((uint64_t)alignedPtr & PageSizeMask), "Not aligned pointer to release RSS memory");
+ if (!numOfPages) {
+ return;
+ }
+ if (auto res = madvise((void*) alignedPtr, numOfPages * PageSize, MADV_DONTNEED); res) {
+ Y_VERIFY(errno == EAGAIN || errno == ENOMEM, "Failed to release memory");
+ }
+ }
+#else
+ void ReleaseRss(char*, uint64_t) noexcept {
+ }
+#endif
+
+#ifdef _linux_
+ uint64_t CountMapped(char* alignedPtr, uint64_t numOfPages) noexcept {
+ Y_VERIFY( !((uint64_t)alignedPtr & PageSizeMask) );
+ Y_ASSERT(numOfPages);
+
+ uint64_t result = 0;
+ unsigned char* mappedPages = (unsigned char*) calloc(numOfPages, numOfPages);
+ Y_VERIFY(mappedPages);
+ Y_DEFER {
+ free(mappedPages);
+ };
+
+ if (!mincore((void*)alignedPtr, numOfPages * PageSize, mappedPages)) {
+ for (uint64_t i = 0; i < numOfPages; ++i) {
+ if (mappedPages[i] & 1) {
+ ++result;
+ }
+ }
+ } else {
+ Y_ASSERT(false);
+ return 0;
+ }
+
+ return result;
+ }
+
+#else
+ uint64_t CountMapped(char*, uint64_t) noexcept {
+ return 0; // stub for Windows tests
+ }
+#endif
+
+}