Skip to content

Commit 87530be

Browse files
authored
Add new UT for IC memory consumption (#11546)
1 parent 64458bf commit 87530be

File tree

3 files changed

+72
-5
lines changed

3 files changed

+72
-5
lines changed

ydb/library/actors/core/events.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ namespace NActors {
7171
return serializer->WriteString(&Blob);
7272
}
7373

74+
virtual ui32 CalculateSerializedSize() const override {
75+
return Blob.size();
76+
}
77+
7478
static IEventBase* Load(TEventSerializedData* bufs) noexcept {
7579
return new TEvBlob(bufs->GetString());
7680
}

ydb/library/actors/interconnect/event_holder_pool.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@ namespace NActors {
3737
class TEventHolderPool {
3838
using TDestroyCallback = std::function<void(THolder<IEventBase>)>;
3939

40-
static constexpr size_t MaxFreeQueueItems = 32;
41-
static constexpr size_t FreeQueueTrimThreshold = MaxFreeQueueItems * 2;
42-
static constexpr ui64 MaxBytesPerMessage = 10 * 1024 * 1024;
43-
4440
TIntrusivePtr<TInterconnectProxyCommon> Common;
4541
std::list<TEventHolder> Cache;
4642
THolder<TEvFreeItems> PendingFreeEvent;
4743
TDestroyCallback DestroyCallback;
4844

4945
public:
46+
static constexpr size_t MaxFreeQueueItems = 32;
47+
static constexpr size_t FreeQueueTrimThreshold = MaxFreeQueueItems * 2;
48+
static constexpr ui64 MaxBytesPerMessage = 10 * 1024 * 1024;
49+
5050
TEventHolderPool(TIntrusivePtr<TInterconnectProxyCommon> common,
5151
TDestroyCallback destroyCallback)
5252
: Common(std::move(common))

ydb/library/actors/interconnect/ut/event_holder_pool_ut.cpp

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
#include <library/cpp/malloc/api/malloc.h>
2+
#include <library/cpp/monlib/dynamic_counters/counters.h>
13
#include <library/cpp/testing/unittest/registar.h>
24
#include <ydb/library/actors/core/events.h>
35
#include <ydb/library/actors/core/event_local.h>
46
#include <ydb/library/actors/interconnect/interconnect_common.h>
5-
#include <library/cpp/monlib/dynamic_counters/counters.h>
67
#include <ydb/library/actors/interconnect/event_holder_pool.h>
78

9+
#include <contrib/libs/tcmalloc/tcmalloc/malloc_extension.h>
10+
811
#include <atomic>
912

1013
using namespace NActors;
@@ -56,4 +59,64 @@ Y_UNIT_TEST_SUITE(EventHolderPool) {
5659
freeQ.clear(); // if we don't this, we may probablty crash due to the order of object destruction
5760
}
5861

62+
struct TMemProfiler {
63+
size_t UsedAtStart = 0;
64+
65+
66+
TMemProfiler()
67+
: UsedAtStart(0)
68+
{
69+
UsedAtStart = GetUsed();
70+
71+
const auto &info = NMalloc::MallocInfo();
72+
bool tcmallocIsUsed = TStringBuf(info.Name).StartsWith("tc");
73+
UNIT_ASSERT(tcmallocIsUsed);
74+
}
75+
76+
size_t GetUsed() {
77+
auto properties = tcmalloc::MallocExtension::GetProperties();
78+
auto x = properties["generic.bytes_in_use_by_app"];
79+
return x.value - UsedAtStart;
80+
}
81+
};
82+
83+
void MemComsumption(size_t repeats, size_t buffSize) {
84+
TDeque<THolder<IEventBase>> freeQ;
85+
auto callback = [&](THolder<IEventBase> event) {
86+
freeQ.push_back(std::move(event));
87+
};
88+
auto pool = Setup(std::move(callback));
89+
90+
std::list<TEventHolder> q;
91+
TMemProfiler prof;
92+
93+
for (ui32 i = 0; i < repeats; i++) {
94+
TEventHolder& event = pool.Allocate(q);
95+
TString data = TString::Uninitialized(buffSize);
96+
auto holder = MakeHolder<IEventHandle>(TActorId{}, TActorId{}, new TEvents::TEvBlob(data));
97+
event.Fill(*holder);
98+
99+
pool.Release(q, q.begin());
100+
UNIT_ASSERT_LT_C(prof.GetUsed(), TEventHolderPool::MaxBytesPerMessage * 2, prof.GetUsed());
101+
}
102+
103+
for (ui32 i = 0; i < repeats; i++) {
104+
TEventHolder& event = pool.Allocate(q);
105+
TString data = TString::Uninitialized(buffSize);
106+
auto holder = MakeHolder<IEventHandle>(TActorId{}, TActorId{}, new TEvents::TEvBlob(data));
107+
event.Fill(*holder);
108+
}
109+
for (ui32 i = 0; i < repeats; i++) {
110+
pool.Release(q, q.begin());
111+
}
112+
UNIT_ASSERT_LT_C(prof.GetUsed(), TEventHolderPool::MaxBytesPerMessage * 2, prof.GetUsed());
113+
}
114+
115+
Y_UNIT_TEST(MemConsumptionSmall) {
116+
MemComsumption(100'000, 4);
117+
}
118+
119+
Y_UNIT_TEST(MemConsumptionLarge) {
120+
MemComsumption(10'000, 1024*1024);
121+
}
59122
}

0 commit comments

Comments
 (0)