Skip to content

Commit e9eb180

Browse files
author
ilezhankin
committed
Provide memory maps count if mmap or unmap fails
commit_hash:12575e4728aabf642632d8681f002d151b202d17
1 parent 7179d8d commit e9eb180

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

yql/essentials/minikql/aligned_page_pool.cpp

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "aligned_page_pool.h"
2-
#include "util/string/builder.h"
32

43
#include <util/generic/yexception.h>
4+
#include <util/stream/file.h>
55
#include <util/string/cast.h>
66
#include <util/system/align.h>
77
#include <util/system/compiler.h>
@@ -26,6 +26,30 @@ static_assert(MaxMidSize == 64 * 1024 * 1024, "Upper memory block 64 Mb");
2626

2727
namespace {
2828

29+
size_t GetMemoryMapsCount() {
30+
size_t lineCount = 0;
31+
TString line;
32+
#if defined(_unix_)
33+
TFileInput file("/proc/self/maps");
34+
while (file.ReadLine(line)) ++lineCount;
35+
#endif
36+
return lineCount;
37+
}
38+
39+
ui64 GetMaxMemoryMaps() {
40+
ui64 maxMapCount = 0;
41+
#if defined(_unix_)
42+
maxMapCount = FromString<ui64>(TFileInput("/proc/sys/vm/max_map_count").ReadAll());
43+
#endif
44+
return maxMapCount;
45+
}
46+
47+
TString GetMemoryMapsString() {
48+
TStringStream ss;
49+
ss << " (maps: " << GetMemoryMapsCount() << " vs " << GetMaxMemoryMaps() << ")";
50+
return ss.Str();
51+
}
52+
2953
template<typename T, bool SysAlign>
3054
class TGlobalPools;
3155

@@ -146,9 +170,15 @@ class TGlobalPools {
146170

147171
void DoMunmap(void* addr, size_t size) {
148172
if (Y_UNLIKELY(0 != T::Munmap(addr, size))) {
173+
TStringStream mmaps;
174+
const auto lastError = LastSystemError();
175+
if (lastError == ENOMEM) {
176+
mmaps << GetMemoryMapsString();
177+
}
178+
149179
ythrow yexception() << "Munmap(0x"
150180
<< IntToString<16>(reinterpret_cast<uintptr_t>(addr))
151-
<< ", " << size << ") failed: " << LastSystemErrorText();
181+
<< ", " << size << ") failed: " << LastSystemErrorText(lastError) << mmaps.Str();
152182
}
153183

154184
i64 prev = TotalMmappedBytes.fetch_sub(size);
@@ -493,7 +523,14 @@ void* TAlignedPagePoolImpl<T>::Alloc(size_t size) {
493523
auto allocSize = size + ALLOC_AHEAD_PAGES * POOL_PAGE_SIZE;
494524
void* mem = globalPool.DoMmap(allocSize);
495525
if (Y_UNLIKELY(MAP_FAILED == mem)) {
496-
ythrow yexception() << "Mmap failed to allocate " << (size + POOL_PAGE_SIZE) << " bytes: " << LastSystemErrorText();
526+
TStringStream mmaps;
527+
const auto lastError = LastSystemError();
528+
if (lastError == ENOMEM) {
529+
mmaps << GetMemoryMapsString();
530+
}
531+
532+
ythrow yexception() << "Mmap failed to allocate " << (size + POOL_PAGE_SIZE) << " bytes: "
533+
<< LastSystemErrorText(lastError) << mmaps.Str();
497534
}
498535

499536
res = AlignUp(mem, POOL_PAGE_SIZE);
@@ -641,7 +678,13 @@ void* GetAlignedPage(ui64 size) {
641678
auto allocSize = Max<ui64>(MaxMidSize, size);
642679
void* mem = pool.DoMmap(allocSize);
643680
if (Y_UNLIKELY(MAP_FAILED == mem)) {
644-
ythrow yexception() << "Mmap failed to allocate " << allocSize << " bytes: " << LastSystemErrorText();
681+
TStringStream mmaps;
682+
const auto lastError = LastSystemError();
683+
if (lastError == ENOMEM) {
684+
mmaps << GetMemoryMapsString();
685+
}
686+
687+
ythrow yexception() << "Mmap failed to allocate " << allocSize << " bytes: " << LastSystemErrorText(lastError) << mmaps.Str();
645688
}
646689

647690
if (size < MaxMidSize) {

0 commit comments

Comments
 (0)