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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
|
#include "aligned_page_pool.h"
#include "util/string/builder.h"
#include <util/generic/yexception.h>
#include <util/string/cast.h>
#include <util/system/align.h>
#include <util/system/compiler.h>
#include <util/system/info.h>
#include <util/system/error.h>
#include <util/thread/lfstack.h>
#if defined(_win_)
#include <util/system/winint.h>
#elif defined(_unix_)
#include <sys/types.h>
#include <sys/mman.h>
#endif
namespace NKikimr {
static ui64 SYS_PAGE_SIZE = NSystemInfo::GetPageSize();
constexpr ui32 MidLevels = 10;
constexpr ui32 MaxMidSize = (1u << MidLevels) * TAlignedPagePool::POOL_PAGE_SIZE;
static_assert(MaxMidSize == 64 * 1024 * 1024, "Upper memory block 64 Mb");
namespace {
template<typename T, bool SysAlign>
class TGlobalPools;
template<typename T, bool SysAlign>
class TGlobalPagePool {
friend class TGlobalPools<T, SysAlign>;
public:
TGlobalPagePool(size_t pageSize)
: PageSize(pageSize)
{}
~TGlobalPagePool() {
void* addr = nullptr;
while (Pages.Dequeue(&addr)) {
FreePage(addr);
}
}
void* GetPage() {
void *page = nullptr;
if (Pages.Dequeue(&page)) {
--Count;
return page;
}
return nullptr;
}
ui64 GetPageCount() const {
return Count.load(std::memory_order_relaxed);
}
size_t GetPageSize() const {
return PageSize;
}
size_t GetSize() const {
return GetPageCount() * GetPageSize();
}
private:
size_t PushPage(void* addr) {
#ifdef PROFILE_MEMORY_ALLOCATIONS
FreePage(addr);
return GetPageSize();
#else
++Count;
Pages.Enqueue(addr);
return 0;
#endif
}
void FreePage(void* addr) {
auto res = T::Munmap(addr, PageSize);
Y_DEBUG_ABORT_UNLESS(0 == res, "Munmap failed: %s", LastSystemErrorText());
}
private:
const size_t PageSize;
std::atomic<ui64> Count = 0;
TLockFreeStack<void*> Pages;
};
template<typename T, bool SysAlign>
class TGlobalPools {
public:
static TGlobalPools<T, SysAlign>& Instance() {
return *Singleton<TGlobalPools<T, SysAlign>>();
}
TGlobalPagePool<T, SysAlign>& Get(ui32 index) {
return *Pools[index];
}
const TGlobalPagePool<T, SysAlign>& Get(ui32 index) const {
return *Pools[index];
}
TGlobalPools()
{
Reset();
}
void* DoMmap(size_t size) {
void* res = T::Mmap(size);
TotalMmappedBytes += size;
return res;
}
void DoCleanupFreeList(ui64 targetSize) {
for(ui32 level = 0; level <= MidLevels; ++level) {
auto& p = Get(level);
const size_t pageSize = p.GetPageSize();
while(p.GetSize() >= targetSize) {
void* page = p.GetPage();
if (!page)
break;
p.FreePage(page);
i64 prev = TotalMmappedBytes.fetch_sub(pageSize);
Y_DEBUG_ABORT_UNLESS(prev >= 0);
}
}
}
void PushPage(size_t level, void* addr) {
auto& pool = Get(level);
size_t free = pool.PushPage(addr);
if (Y_UNLIKELY(free > 0)) {
i64 prev = TotalMmappedBytes.fetch_sub(free);
Y_DEBUG_ABORT_UNLESS(prev >= 0);
}
}
void DoMunmap(void* addr, size_t size) {
if (Y_UNLIKELY(0 != T::Munmap(addr, size))) {
ythrow yexception() << "Munmap(0x"
<< IntToString<16>(reinterpret_cast<uintptr_t>(addr))
<< ", " << size << ") failed: " << LastSystemErrorText();
}
i64 prev = TotalMmappedBytes.fetch_sub(size);
Y_DEBUG_ABORT_UNLESS(prev >= 0);
}
i64 GetTotalMmappedBytes() const {
return TotalMmappedBytes.load();
}
i64 GetTotalFreeListBytes() const {
i64 bytes = 0;
for (ui32 i = 0; i <= MidLevels; ++i) {
bytes += Get(i).GetSize();
}
return bytes;
}
void Reset()
{
Pools.clear();
Pools.reserve(MidLevels + 1);
for (ui32 i = 0; i <= MidLevels; ++i) {
Pools.emplace_back(MakeHolder<TGlobalPagePool<T, SysAlign>>(TAlignedPagePool::POOL_PAGE_SIZE << i));
}
}
private:
TVector<THolder<TGlobalPagePool<T, SysAlign>>> Pools;
std::atomic<i64> TotalMmappedBytes{0};
};
} // unnamed
#ifdef _win_
#define MAP_FAILED (void*)(-1)
inline void* TSystemMmap::Mmap(size_t size)
{
if (auto res = ::VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)) {
return res;
} else {
return MAP_FAILED;
}
}
inline int TSystemMmap::Munmap(void* addr, size_t size)
{
Y_ABORT_UNLESS(AlignUp(addr, SYS_PAGE_SIZE) == addr, "Got unaligned address");
Y_ABORT_UNLESS(AlignUp(size, SYS_PAGE_SIZE) == size, "Got unaligned size");
return !::VirtualFree(addr, size, MEM_DECOMMIT);
}
#else
inline void* TSystemMmap::Mmap(size_t size)
{
return ::mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, 0, 0);
}
inline int TSystemMmap::Munmap(void* addr, size_t size)
{
Y_DEBUG_ABORT_UNLESS(AlignUp(addr, SYS_PAGE_SIZE) == addr, "Got unaligned address");
Y_DEBUG_ABORT_UNLESS(AlignUp(size, SYS_PAGE_SIZE) == size, "Got unaligned size");
return ::munmap(addr, size);
}
#endif
std::function<void(size_t size)> TFakeAlignedMmap::OnMmap = {};
std::function<void(void* addr, size_t size)> TFakeAlignedMmap::OnMunmap = {};
void* TFakeAlignedMmap::Mmap(size_t size)
{
if (OnMmap) {
OnMmap(size);
}
return reinterpret_cast<void*>(TAlignedPagePool::POOL_PAGE_SIZE);
}
int TFakeAlignedMmap::Munmap(void* addr, size_t size)
{
if (OnMunmap) {
OnMunmap(addr, size);
}
return 0;
}
std::function<void(size_t size)> TFakeUnalignedMmap::OnMmap = {};
std::function<void(void* addr, size_t size)> TFakeUnalignedMmap::OnMunmap = {};
void* TFakeUnalignedMmap::Mmap(size_t size)
{
if (OnMmap) {
OnMmap(size);
}
return reinterpret_cast<void*>(TAlignedPagePool::POOL_PAGE_SIZE+1);
}
int TFakeUnalignedMmap::Munmap(void* addr, size_t size)
{
if (OnMunmap) {
OnMunmap(addr, size);
}
return 0;
}
TAlignedPagePoolCounters::TAlignedPagePoolCounters(::NMonitoring::TDynamicCounterPtr countersRoot, const TString& name) {
if (!countersRoot || name.empty())
return;
::NMonitoring::TDynamicCounterPtr subGroup = countersRoot->GetSubgroup("counters", "utils")->GetSubgroup("subsystem", "mkqlalloc");
TotalBytesAllocatedCntr = subGroup->GetCounter(name + "/TotalBytesAllocated");
AllocationsCntr = subGroup->GetCounter(name + "/Allocations", true);
PoolsCntr = subGroup->GetCounter(name + "/Pools", true);
LostPagesBytesFreeCntr = subGroup->GetCounter(name + "/LostPagesBytesFreed", true);
}
template<typename T>
TAlignedPagePoolImpl<T>::~TAlignedPagePoolImpl() {
if (CheckLostMem && !UncaughtException()) {
Y_DEBUG_ABORT_UNLESS(TotalAllocated == FreePages.size() * POOL_PAGE_SIZE,
"memory leak; Expected %ld, actual %ld (%ld page(s), %ld offloaded); allocator created at: %s",
TotalAllocated, FreePages.size() * POOL_PAGE_SIZE,
FreePages.size(), OffloadedActiveBytes, GetDebugInfo().data());
Y_DEBUG_ABORT_UNLESS(OffloadedActiveBytes == 0, "offloaded: %ld", OffloadedActiveBytes);
}
size_t activeBlocksSize = 0;
for (auto it = ActiveBlocks.cbegin(); ActiveBlocks.cend() != it; ActiveBlocks.erase(it++)) {
activeBlocksSize += it->second;
#ifdef PROFILE_MEMORY_ALLOCATIONS
ReturnBlock(it->first, it->second);
#else
Free(it->first, it->second);
#endif
}
if (activeBlocksSize > 0 || FreePages.size() != AllPages.size() || OffloadedActiveBytes) {
if (Counters.LostPagesBytesFreeCntr) {
(*Counters.LostPagesBytesFreeCntr) += OffloadedActiveBytes + activeBlocksSize + (AllPages.size() - FreePages.size()) * POOL_PAGE_SIZE;
}
}
Y_DEBUG_ABORT_UNLESS(TotalAllocated == AllPages.size() * POOL_PAGE_SIZE + OffloadedActiveBytes,
"Expected %ld, actual %ld (%ld page(s))", TotalAllocated,
AllPages.size() * POOL_PAGE_SIZE + OffloadedActiveBytes, AllPages.size());
for (auto &ptr : AllPages) {
TGlobalPools<T, false>::Instance().PushPage(0, ptr);
}
if (Counters.TotalBytesAllocatedCntr) {
(*Counters.TotalBytesAllocatedCntr) -= TotalAllocated;
}
if (Counters.PoolsCntr) {
--(*Counters.PoolsCntr);
}
TotalAllocated = 0;
}
template<typename T>
void TAlignedPagePoolImpl<T>::ReleaseFreePages() {
TotalAllocated -= FreePages.size() * POOL_PAGE_SIZE;
if (Counters.TotalBytesAllocatedCntr) {
(*Counters.TotalBytesAllocatedCntr) -= FreePages.size() * POOL_PAGE_SIZE;
}
for (; !FreePages.empty(); FreePages.pop()) {
AllPages.erase(FreePages.top());
TGlobalPools<T, false>::Instance().PushPage(0, FreePages.top());
}
}
template<typename T>
void TAlignedPagePoolImpl<T>::OffloadAlloc(ui64 size) {
if (Limit && TotalAllocated + size > Limit && !TryIncreaseLimit(TotalAllocated + size)) {
throw TMemoryLimitExceededException();
}
if (AllocNotifyCallback) {
if (AllocNotifyCurrentBytes > AllocNotifyBytes) {
AllocNotifyCallback();
AllocNotifyCurrentBytes = 0;
}
}
++OffloadedAllocCount;
OffloadedBytes += size;
OffloadedActiveBytes += size;
TotalAllocated += size;
if (AllocNotifyCallback) {
AllocNotifyCurrentBytes += size;
}
if (Counters.TotalBytesAllocatedCntr) {
(*Counters.TotalBytesAllocatedCntr) += size;
}
if (Counters.AllocationsCntr) {
++(*Counters.AllocationsCntr);
}
UpdatePeaks();
}
template<typename T>
void TAlignedPagePoolImpl<T>::OffloadFree(ui64 size) noexcept {
TotalAllocated -= size;
OffloadedActiveBytes -= size;
if (Counters.TotalBytesAllocatedCntr) {
(*Counters.TotalBytesAllocatedCntr) -= size;
}
}
template<typename T>
void* TAlignedPagePoolImpl<T>::GetPage() {
++PageAllocCount;
if (!FreePages.empty()) {
++PageHitCount;
const auto res = FreePages.top();
FreePages.pop();
return res;
}
if (Limit && TotalAllocated + POOL_PAGE_SIZE > Limit && !TryIncreaseLimit(TotalAllocated + POOL_PAGE_SIZE)) {
throw TMemoryLimitExceededException();
}
#ifndef PROFILE_MEMORY_ALLOCATIONS
if (const auto ptr = TGlobalPools<T, false>::Instance().Get(0).GetPage()) {
TotalAllocated += POOL_PAGE_SIZE;
if (AllocNotifyCallback) {
AllocNotifyCurrentBytes += POOL_PAGE_SIZE;
}
if (Counters.TotalBytesAllocatedCntr) {
(*Counters.TotalBytesAllocatedCntr) += POOL_PAGE_SIZE;
}
++PageGlobalHitCount;
AllPages.emplace(ptr);
UpdatePeaks();
return ptr;
}
++PageMissCount;
#endif
#ifdef PROFILE_MEMORY_ALLOCATIONS
const auto res = GetBlock(POOL_PAGE_SIZE);
#else
const auto res = Alloc(POOL_PAGE_SIZE);
AllPages.emplace(res);
#endif
return res;
}
template<typename T>
void TAlignedPagePoolImpl<T>::ReturnPage(void* addr) noexcept {
#ifdef PROFILE_MEMORY_ALLOCATIONS
ReturnBlock(addr, POOL_PAGE_SIZE);
#else
Y_DEBUG_ABORT_UNLESS(AllPages.find(addr) != AllPages.end());
FreePages.emplace(addr);
#endif
}
template<typename T>
void* TAlignedPagePoolImpl<T>::GetBlock(size_t size) {
Y_DEBUG_ABORT_UNLESS(size >= POOL_PAGE_SIZE);
#ifdef PROFILE_MEMORY_ALLOCATIONS
OffloadAlloc(size);
auto ret = malloc(size);
if (!ret) {
throw TMemoryLimitExceededException();
}
return ret;
#else
if (size == POOL_PAGE_SIZE) {
return GetPage();
} else {
const auto ptr = Alloc(size);
Y_DEBUG_ABORT_UNLESS(ActiveBlocks.emplace(ptr, size).second);
return ptr;
}
#endif
}
template<typename T>
void TAlignedPagePoolImpl<T>::ReturnBlock(void* ptr, size_t size) noexcept {
Y_DEBUG_ABORT_UNLESS(size >= POOL_PAGE_SIZE);
#ifdef PROFILE_MEMORY_ALLOCATIONS
OffloadFree(size);
free(ptr);
#else
if (size == POOL_PAGE_SIZE) {
ReturnPage(ptr);
} else {
Free(ptr, size);
Y_DEBUG_ABORT_UNLESS(ActiveBlocks.erase(ptr));
}
#endif
UpdateMemoryYellowZone();
}
template<typename T>
void* TAlignedPagePoolImpl<T>::Alloc(size_t size) {
void* res = nullptr;
size = AlignUp(size, SYS_PAGE_SIZE);
if (Limit && TotalAllocated + size > Limit && !TryIncreaseLimit(TotalAllocated + size)) {
throw TMemoryLimitExceededException();
}
if (AllocNotifyCallback) {
if (AllocNotifyCurrentBytes > AllocNotifyBytes) {
AllocNotifyCallback();
AllocNotifyCurrentBytes = 0;
}
}
auto& globalPool = TGlobalPools<T, false>::Instance();
if (size > POOL_PAGE_SIZE && size <= MaxMidSize) {
size = FastClp2(size);
auto level = LeastSignificantBit(size) - LeastSignificantBit(POOL_PAGE_SIZE);
Y_DEBUG_ABORT_UNLESS(level >= 1 && level <= MidLevels);
if (res = globalPool.Get(level).GetPage()) {
TotalAllocated += size;
if (AllocNotifyCallback) {
AllocNotifyCurrentBytes += size;
}
if (Counters.TotalBytesAllocatedCntr) {
(*Counters.TotalBytesAllocatedCntr) += size;
}
++PageGlobalHitCount;
} else {
++PageMissCount;
}
}
if (!res) {
auto allocSize = size + ALLOC_AHEAD_PAGES * POOL_PAGE_SIZE;
void* mem = globalPool.DoMmap(allocSize);
if (Y_UNLIKELY(MAP_FAILED == mem)) {
ythrow yexception() << "Mmap failed to allocate " << (size + POOL_PAGE_SIZE) << " bytes: " << LastSystemErrorText();
}
res = AlignUp(mem, POOL_PAGE_SIZE);
const size_t off = reinterpret_cast<intptr_t>(res) - reinterpret_cast<intptr_t>(mem);
if (Y_UNLIKELY(off)) {
// unmap prefix
globalPool.DoMunmap(mem, off);
}
// Extra space is also page-aligned. Put it to the free page list
auto alignedSize = AlignUp(size, POOL_PAGE_SIZE);
ui64 extraPages = (allocSize - off - alignedSize) / POOL_PAGE_SIZE;
ui64 tail = (allocSize - off - alignedSize) % POOL_PAGE_SIZE;
auto extraPage = reinterpret_cast<ui8*>(res) + alignedSize;
for (ui64 i = 0; i < extraPages; ++i) {
AllPages.emplace(extraPage);
FreePages.emplace(extraPage);
extraPage += POOL_PAGE_SIZE;
}
if (size != alignedSize) {
// unmap unaligned hole
globalPool.DoMunmap(reinterpret_cast<ui8*>(res) + size, alignedSize - size);
}
if (tail) {
// unmap suffix
Y_DEBUG_ABORT_UNLESS(extraPage+tail <= reinterpret_cast<ui8*>(mem) + size + ALLOC_AHEAD_PAGES * POOL_PAGE_SIZE);
globalPool.DoMunmap(extraPage, tail);
}
auto extraSize = extraPages * POOL_PAGE_SIZE;
auto totalSize = size + extraSize;
TotalAllocated += totalSize;
if (AllocNotifyCallback) {
AllocNotifyCurrentBytes += totalSize;
}
if (Counters.TotalBytesAllocatedCntr) {
(*Counters.TotalBytesAllocatedCntr) += totalSize;
}
}
if (Counters.AllocationsCntr) {
++(*Counters.AllocationsCntr);
}
++AllocCount;
UpdatePeaks();
return res;
}
template<typename T>
void TAlignedPagePoolImpl<T>::Free(void* ptr, size_t size) noexcept {
size = AlignUp(size, SYS_PAGE_SIZE);
if (size <= MaxMidSize)
size = FastClp2(size);
if (size <= MaxMidSize) {
auto level = LeastSignificantBit(size) - LeastSignificantBit(POOL_PAGE_SIZE);
Y_DEBUG_ABORT_UNLESS(level >= 1 && level <= MidLevels);
TGlobalPools<T, false>::Instance().PushPage(level, ptr);
} else {
TGlobalPools<T, false>::Instance().DoMunmap(ptr, size);
}
Y_DEBUG_ABORT_UNLESS(TotalAllocated >= size);
TotalAllocated -= size;
if (Counters.TotalBytesAllocatedCntr) {
(*Counters.TotalBytesAllocatedCntr) -= size;
}
}
template<typename T>
void TAlignedPagePoolImpl<T>::DoCleanupGlobalFreeList(ui64 targetSize) {
TGlobalPools<T, true>::Instance().DoCleanupFreeList(targetSize);
TGlobalPools<T, false>::Instance().DoCleanupFreeList(targetSize);
}
template<typename T>
void TAlignedPagePoolImpl<T>::UpdateMemoryYellowZone() {
if (Limit == 0) return;
if (IsMemoryYellowZoneForcefullyChanged) return;
if (IncreaseMemoryLimitCallback && !IsMaximumLimitValueReached) return;
ui8 usedMemoryPercent = 100 * GetUsed() / Limit;
if (usedMemoryPercent >= EnableMemoryYellowZoneThreshold) {
IsMemoryYellowZoneReached = true;
} else if (usedMemoryPercent <= DisableMemoryYellowZoneThreshold) {
IsMemoryYellowZoneReached = false;
}
}
template<typename T>
bool TAlignedPagePoolImpl<T>::TryIncreaseLimit(ui64 required) {
if (!IncreaseMemoryLimitCallback) {
return false;
}
IncreaseMemoryLimitCallback(Limit, required);
return Limit >= required;
}
template<typename T>
ui64 TAlignedPagePoolImpl<T>::GetGlobalPagePoolSize() {
ui64 size = 0;
for (size_t level = 0; level <= MidLevels; ++level) {
size += TGlobalPools<T, false>::Instance().Get(level).GetSize();
}
return size;
}
template<typename T>
void TAlignedPagePoolImpl<T>::PrintStat(size_t usedPages, IOutputStream& out) const {
usedPages += GetFreePageCount();
out << "Count of free pages: " << GetFreePageCount() << Endl;
out << "Allocated for blocks: " << (GetAllocated() - usedPages * POOL_PAGE_SIZE) << Endl;
out << "Total allocated by lists: " << GetAllocated() << Endl;
}
template<typename T>
void TAlignedPagePoolImpl<T>::ResetGlobalsUT()
{
TGlobalPools<T, false>::Instance().Reset();
}
template class TAlignedPagePoolImpl<>;
template class TAlignedPagePoolImpl<TFakeAlignedMmap>;
template class TAlignedPagePoolImpl<TFakeUnalignedMmap>;
template<typename TMmap>
void* GetAlignedPage(ui64 size) {
size = AlignUp(size, SYS_PAGE_SIZE);
if (size < TAlignedPagePool::POOL_PAGE_SIZE) {
size = TAlignedPagePool::POOL_PAGE_SIZE;
}
auto& pool = TGlobalPools<TMmap, true>::Instance();
if (size <= MaxMidSize) {
size = FastClp2(size);
auto level = LeastSignificantBit(size) - LeastSignificantBit(TAlignedPagePool::POOL_PAGE_SIZE);
Y_DEBUG_ABORT_UNLESS(level <= MidLevels);
if (auto res = pool.Get(level).GetPage()) {
return res;
}
}
auto allocSize = Max<ui64>(MaxMidSize, size);
void* mem = pool.DoMmap(allocSize);
if (Y_UNLIKELY(MAP_FAILED == mem)) {
ythrow yexception() << "Mmap failed to allocate " << allocSize << " bytes: " << LastSystemErrorText();
}
if (size < MaxMidSize) {
// push extra allocated pages to cache
auto level = LeastSignificantBit(size) - LeastSignificantBit(TAlignedPagePool::POOL_PAGE_SIZE);
Y_DEBUG_ABORT_UNLESS(level <= MidLevels);
ui8* ptr = (ui8*)mem + size;
ui8* const end = (ui8*)mem + MaxMidSize;
while (ptr < end) {
pool.PushPage(level, ptr);
ptr += size;
}
}
return mem;
}
template<typename TMmap>
void ReleaseAlignedPage(void* mem, ui64 size) {
size = AlignUp(size, SYS_PAGE_SIZE);
if (size < TAlignedPagePool::POOL_PAGE_SIZE) {
size = TAlignedPagePool::POOL_PAGE_SIZE;
}
if (size <= MaxMidSize) {
size = FastClp2(size);
auto level = LeastSignificantBit(size) - LeastSignificantBit(TAlignedPagePool::POOL_PAGE_SIZE);
Y_DEBUG_ABORT_UNLESS(level <= MidLevels);
TGlobalPools<TMmap, true>::Instance().PushPage(level, mem);
return;
}
TGlobalPools<TMmap, true>::Instance().DoMunmap(mem, size);
}
template<typename TMmap>
i64 GetTotalMmapedBytes() {
return TGlobalPools<TMmap, true>::Instance().GetTotalMmappedBytes() + TGlobalPools<TMmap, false>::Instance().GetTotalMmappedBytes();
}
template<typename TMmap>
i64 GetTotalFreeListBytes() {
return TGlobalPools<TMmap, true>::Instance().GetTotalFreeListBytes() + TGlobalPools<TMmap, false>::Instance().GetTotalFreeListBytes();
}
template i64 GetTotalMmapedBytes<>();
template i64 GetTotalMmapedBytes<TFakeAlignedMmap>();
template i64 GetTotalMmapedBytes<TFakeUnalignedMmap>();
template i64 GetTotalFreeListBytes<>();
template i64 GetTotalFreeListBytes<TFakeAlignedMmap>();
template i64 GetTotalFreeListBytes<TFakeUnalignedMmap>();
template void* GetAlignedPage<>(ui64);
template void* GetAlignedPage<TFakeAlignedMmap>(ui64);
template void* GetAlignedPage<TFakeUnalignedMmap>(ui64);
template void ReleaseAlignedPage<>(void*,ui64);
template void ReleaseAlignedPage<TFakeAlignedMmap>(void*,ui64);
template void ReleaseAlignedPage<TFakeUnalignedMmap>(void*,ui64);
} // NKikimr
|