Skip to content

Commit de92acc

Browse files
author
ilezhankin
committed
Add counter for wasted memory from TPagedBuffer in profiling mode
Пример графика с тестового кластера: <https://nda.ya.ru/t/RdGCM06J7EGx4P> commit_hash:4cf9981cc6d7541487678a189386e8e7f091811d
1 parent 21395e1 commit de92acc

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-14
lines changed

yql/essentials/minikql/mkql_buffer.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
#include "mkql_buffer.h"
22

3-
namespace NKikimr {
3+
namespace NKikimr::NMiniKQL {
44

5-
namespace NMiniKQL {
5+
#if defined(PROFILE_MEMORY_ALLOCATIONS)
6+
NMonitoring::TDynamicCounters::TCounterPtr TotalBytesWastedCounter;
7+
8+
void InitializeGlobalPagedBufferCounters(::NMonitoring::TDynamicCounterPtr root) {
9+
NMonitoring::TDynamicCounterPtr subGroup = root->GetSubgroup("counters", "utils")->GetSubgroup("subsystem", "mkqlalloc");
10+
TotalBytesWastedCounter = subGroup->GetCounter("PagedBuffer/TotalBytesWasted");
11+
}
12+
#endif
613

714
const size_t TBufferPage::PageCapacity = TBufferPage::PageAllocSize - sizeof(TBufferPage);
815

@@ -14,19 +21,25 @@ TBufferPage* TBufferPage::Allocate() {
1421
throw std::bad_alloc();
1522
}
1623
TBufferPage* result = ::new (ptr) TBufferPage();
24+
#if defined(PROFILE_MEMORY_ALLOCATIONS)
25+
TotalBytesWastedCounter->Add(result->Wasted());
26+
#endif
1727
return result;
1828
}
1929

2030
void TBufferPage::Free(TBufferPage* page) {
31+
#if defined(PROFILE_MEMORY_ALLOCATIONS)
32+
TotalBytesWastedCounter->Sub(page->Wasted());
33+
#endif
2134
free(page);
2235
}
2336

2437
void TPagedBuffer::AppendPage() {
2538
TBufferPage* page = nullptr;
2639
if (Tail_) {
2740
auto tailPage = TBufferPage::GetPage(Tail_);
28-
auto next = tailPage->Next();
29-
if (next) {
41+
if (auto next = tailPage->Next()) {
42+
// TODO: can we get here?
3043
page = next;
3144
page->Clear();
3245
} else {
@@ -54,6 +67,4 @@ TChunkedBuffer TPagedBuffer::AsChunkedBuffer(const TConstPtr& buffer) {
5467
return result;
5568
}
5669

57-
} // NMiniKQL
58-
59-
} // NKikimr
70+
} // namespace NKikimr::NMiniKQL

yql/essentials/minikql/mkql_buffer.h

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,22 @@
22

33
#include "defs.h"
44

5+
#if defined(PROFILE_MEMORY_ALLOCATIONS)
6+
# include <library/cpp/monlib/dynamic_counters/counters.h>
7+
#endif
8+
59
#include <yql/essentials/utils/chunked_buffer.h>
610

711
#include <util/generic/noncopyable.h>
812
#include <util/stream/output.h>
913
#include <util/system/yassert.h>
1014

11-
namespace NKikimr {
15+
namespace NKikimr::NMiniKQL {
1216

13-
namespace NMiniKQL {
17+
#if defined(PROFILE_MEMORY_ALLOCATIONS)
18+
extern NMonitoring::TDynamicCounters::TCounterPtr TotalBytesWastedCounter;
19+
void InitializeGlobalPagedBufferCounters(::NMonitoring::TDynamicCounterPtr root);
20+
#endif
1421

1522
class TPagedBuffer;
1623

@@ -48,6 +55,10 @@ class TBufferPage : private TNonCopyable {
4855
Size_ = 0;
4956
}
5057

58+
inline size_t Wasted() const {
59+
return PageCapacity - Size_;
60+
}
61+
5162
private:
5263
TBufferPage* Next_ = nullptr;
5364
size_t Size_ = 0;
@@ -75,6 +86,9 @@ class TPagedBuffer : private TNonCopyable {
7586

7687
~TPagedBuffer() {
7788
if (Head_) {
89+
auto* tailPage = TBufferPage::GetPage(Tail_);
90+
tailPage->Size_ = TailSize_;
91+
7892
TBufferPage* curr = TBufferPage::GetPage(Head_);
7993
while (curr) {
8094
auto drop = curr;
@@ -117,7 +131,6 @@ class TPagedBuffer : private TNonCopyable {
117131
}
118132

119133
inline size_t Size() const {
120-
// + (Tail_ ? TailSize_ : 0);
121134
size_t sizeWithReserve = ClosedPagesSize_ + ((-size_t(Tail_ != nullptr)) & TailSize_);
122135
Y_DEBUG_ABORT_UNLESS(sizeWithReserve >= HeadReserve_);
123136
return sizeWithReserve - HeadReserve_;
@@ -171,26 +184,35 @@ class TPagedBuffer : private TNonCopyable {
171184
}
172185

173186
inline void Clear() {
187+
// TODO: not wasted or never called?
174188
Tail_ = Head_;
175189
ClosedPagesSize_ = HeadReserve_ = 0;
176-
// = Tail_ ? 0 : TBufferPage::PageAllocSize;
177190
TailSize_ = (-size_t(Tail_ == nullptr)) & TBufferPage::PageCapacity;
178191
}
179192

180193
inline void EraseBack(size_t len) {
181194
Y_DEBUG_ABORT_UNLESS(Tail_ && TailSize_ >= len);
182195
TailSize_ -= len;
196+
#if defined(PROFILE_MEMORY_ALLOCATIONS)
197+
TotalBytesWastedCounter->Add(len);
198+
#endif
183199
}
184200

185201
inline void Advance(size_t len) {
186202
if (Y_LIKELY(TailSize_ + len <= TBufferPage::PageCapacity)) {
187203
TailSize_ += len;
204+
#if defined(PROFILE_MEMORY_ALLOCATIONS)
205+
TotalBytesWastedCounter->Sub(len);
206+
#endif
188207
return;
189208
}
190209

191210
MKQL_ENSURE(len <= TBufferPage::PageCapacity, "Advance() size too big");
192211
AppendPage();
193212
TailSize_ = len;
213+
#if defined(PROFILE_MEMORY_ALLOCATIONS)
214+
TotalBytesWastedCounter->Sub(len);
215+
#endif
194216
}
195217

196218
inline void Append(char c) {
@@ -211,6 +233,9 @@ class TPagedBuffer : private TNonCopyable {
211233
TailSize_ += chunk;
212234
data += chunk;
213235
size -= chunk;
236+
#if defined(PROFILE_MEMORY_ALLOCATIONS)
237+
TotalBytesWastedCounter->Sub(chunk);
238+
#endif
214239
}
215240
}
216241

@@ -227,6 +252,4 @@ class TPagedBuffer : private TNonCopyable {
227252
size_t ClosedPagesSize_ = 0;
228253
};
229254

230-
} // NMiniKQL
231-
232-
} // NKikimr
255+
} // namespace NKikimr::NMiniKQL

0 commit comments

Comments
 (0)