1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
}
|