aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/core/balancer_ut.cpp
blob: 7e5e95f4b972c20b63aeb2dd7151635778cf9624 (plain) (blame)
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
#include "balancer.h"

#include <library/cpp/actors/util/datetime.h>
#include <library/cpp/lwtrace/all.h>
#include <library/cpp/testing/unittest/registar.h>

#include <util/stream/str.h>

using namespace NActors;

////////////////////////////////////////////////////////////////////////////////

Y_UNIT_TEST_SUITE(PoolCpuBalancer) {
    struct TTest {
        TCpuManagerConfig Config;
        TCpuMask Available;
        THolder<IBalancer> Balancer;
        TVector<TCpuState> CpuStates;
        TVector<ui64> CpuUs;
        ui64 Now = 0;

        void SetCpuCount(size_t count) {
            Config.UnitedWorkers.CpuCount = count;
            for (TCpuId cpuId = 0; cpuId < count; cpuId++) {
                Available.Set(cpuId);
            }
        }

        void AddPool(ui32 minCpus, ui32 cpus, ui32 maxCpus, ui8 priority = 0) {
            TUnitedExecutorPoolConfig u;
            u.PoolId = TPoolId(Config.United.size());
            u.Balancing.Cpus = cpus;
            u.Balancing.MinCpus = minCpus;
            u.Balancing.MaxCpus = maxCpus;
            u.Balancing.Priority = priority;
            Config.United.push_back(u);
        }

        void Start() {
            TCpuAllocationConfig allocation(Available, Config);
            Balancer.Reset(MakeBalancer(Config.UnitedWorkers.Balancer, Config.United, 0));
            CpuStates.resize(allocation.Items.size()); // do not resize it later to avoid dangling pointers
            CpuUs.resize(CpuStates.size());
            for (const TCpuAllocation& cpuAlloc : allocation.Items) {
                bool added = Balancer->AddCpu(cpuAlloc, &CpuStates[cpuAlloc.CpuId]);
                UNIT_ASSERT(added);
            }
        }

        void Balance(ui64 deltaTs, const TVector<ui64>& cpuUs) {
            Now += deltaTs;
            ui64 ts = Now;
            if (Balancer->TryLock(ts)) {
                for (TPoolId pool = 0; pool < cpuUs.size(); pool++) {
                    CpuUs[pool] += cpuUs[pool];
                    TBalancerStats stats;
                    stats.Ts = ts;
                    stats.CpuUs = CpuUs[pool];
                    Balancer->SetPoolStats(pool, stats);
                }
                Balancer->Balance();
                Balancer->Unlock();
            }
        }

        void ApplyMovements() {
            for (TCpuState& state : CpuStates) {
                TPoolId current;
                TPoolId assigned;
                state.Load(assigned, current);
                state.SwitchPool(assigned);
            }
        }

        static TString ToStr(const TVector<ui64>& values) {
            TStringStream ss;
            ss << "{";
            for (auto v : values) {
                ss << " " << v;
            }
            ss << " }";
            return ss.Str();
        }

        void AssertPoolsCurrentCpus(const TVector<ui64>& cpuRequired) {
            TVector<ui64> cpuCurrent;
            cpuCurrent.resize(cpuRequired.size());
            for (TCpuState& state : CpuStates) {
                TPoolId current;
                TPoolId assigned;
                state.Load(assigned, current);
                cpuCurrent[current]++;
            }
            for (TPoolId pool = 0; pool < cpuRequired.size(); pool++) {
                UNIT_ASSERT_C(cpuCurrent[pool] == cpuRequired[pool],
                    "cpu distribution mismatch, required " << ToStr(cpuRequired) << " but got " << ToStr(cpuCurrent));
            }
        }
    };

    Y_UNIT_TEST(StartLwtrace) {
        NLWTrace::StartLwtraceFromEnv();
    }

    Y_UNIT_TEST(AllOverloaded) {
        TTest t;
        int cpus = 10;
        t.SetCpuCount(cpus);
        t.AddPool(1, 1, 10); // pool=0
        t.AddPool(1, 2, 10); // pool=1
        t.AddPool(1, 3, 10); // pool=2
        t.AddPool(1, 4, 10); // pool=2
        t.Start();
        ui64 dts = 1.01 * Us2Ts(t.Config.UnitedWorkers.Balancer.PeriodUs);
        ui64 totalCpuUs = cpus * Ts2Us(dts); // pretend every pool has consumed as whole actorsystem, overload
        for (int i = 0; i < cpus; i++) {
            t.Balance(dts, {totalCpuUs, totalCpuUs, totalCpuUs, totalCpuUs});
            t.ApplyMovements();
        }
        t.AssertPoolsCurrentCpus({1, 2, 3, 4});
    }

    Y_UNIT_TEST(OneOverloaded) {
        TTest t;
        int cpus = 10;
        t.SetCpuCount(cpus);
        t.AddPool(1, 1, 10); // pool=0
        t.AddPool(1, 2, 10); // pool=1
        t.AddPool(1, 3, 10); // pool=2
        t.AddPool(1, 4, 10); // pool=2
        t.Start();
        ui64 dts = 1.01 * Us2Ts(t.Config.UnitedWorkers.Balancer.PeriodUs);
        ui64 totalCpuUs = cpus * Ts2Us(dts);
        for (int i = 0; i < cpus; i++) {
            t.Balance(dts, {totalCpuUs, 0, 0, 0});
            t.ApplyMovements();
        }
        t.AssertPoolsCurrentCpus({7, 1, 1, 1});
        for (int i = 0; i < cpus; i++) {
            t.Balance(dts, {0, totalCpuUs, 0, 0});
            t.ApplyMovements();
        }
        t.AssertPoolsCurrentCpus({1, 7, 1, 1});
        for (int i = 0; i < cpus; i++) {
            t.Balance(dts, {0, 0, totalCpuUs, 0});
            t.ApplyMovements();
        }
        t.AssertPoolsCurrentCpus({1, 1, 7, 1});
        for (int i = 0; i < cpus; i++) {
            t.Balance(dts, {0, 0, 0, totalCpuUs});
            t.ApplyMovements();
        }
        t.AssertPoolsCurrentCpus({1, 1, 1, 7});
    }

    Y_UNIT_TEST(TwoOverloadedFairness) {
        TTest t;
        int cpus = 10;
        t.SetCpuCount(cpus);
        t.AddPool(1, 1, 10); // pool=0
        t.AddPool(1, 2, 10); // pool=1
        t.AddPool(1, 3, 10); // pool=2
        t.AddPool(1, 4, 10); // pool=2
        t.Start();
        ui64 dts = 1.01 * Us2Ts(t.Config.UnitedWorkers.Balancer.PeriodUs);
        ui64 totalCpuUs = cpus * Ts2Us(dts);
        for (int i = 0; i < cpus; i++) {
            t.Balance(dts, {totalCpuUs, totalCpuUs, 0, 0});
            t.ApplyMovements();
        }
        t.AssertPoolsCurrentCpus({3, 5, 1, 1});
        for (int i = 0; i < cpus; i++) {
            t.Balance(dts, {totalCpuUs, 0, totalCpuUs, 0});
            t.ApplyMovements();
        }
        t.AssertPoolsCurrentCpus({2, 1, 6, 1});
        for (int i = 0; i < cpus; i++) {
            t.Balance(dts, {totalCpuUs, 0, 0, totalCpuUs});
            t.ApplyMovements();
        }
        t.AssertPoolsCurrentCpus({2, 1, 1, 6});
        for (int i = 0; i < cpus; i++) {
            t.Balance(dts, {0, totalCpuUs, totalCpuUs, 0});
            t.ApplyMovements();
        }
        t.AssertPoolsCurrentCpus({1, 3, 5, 1});
        for (int i = 0; i < cpus; i++) {
            t.Balance(dts, {0, totalCpuUs, 0, totalCpuUs});
            t.ApplyMovements();
        }
        t.AssertPoolsCurrentCpus({1, 3, 1, 5});
        for (int i = 0; i < cpus; i++) {
            t.Balance(dts, {0, 0, totalCpuUs, totalCpuUs});
            t.ApplyMovements();
        }
        t.AssertPoolsCurrentCpus({1, 1, 3, 5});
    }

    Y_UNIT_TEST(TwoOverloadedPriority) {
        TTest t;
        int cpus = 20;
        t.SetCpuCount(cpus);
        t.AddPool(1, 5, 20, 0); // pool=0
        t.AddPool(1, 5, 20, 1); // pool=1
        t.AddPool(1, 5, 20, 2); // pool=2
        t.AddPool(1, 5, 20, 3); // pool=3
        t.Start();
        ui64 dts = 1.01 * Us2Ts(t.Config.UnitedWorkers.Balancer.PeriodUs);
        ui64 mErlang = Ts2Us(dts) / 1000;
        for (int i = 0; i < cpus; i++) {
            t.Balance(dts, {20000 * mErlang, 2500 * mErlang, 4500 * mErlang, 9500 * mErlang});
            t.ApplyMovements();
        }
        t.AssertPoolsCurrentCpus({2, 3, 5, 10});
        t.Balance(dts, {20000 * mErlang, 2500 * mErlang, 4500 * mErlang, 8500 * mErlang});
        t.ApplyMovements();
        t.AssertPoolsCurrentCpus({3, 3, 5, 9});
        // NOTE: this operation require one move, but we do not make global analysis, so multiple steps (1->2 & 0->1) are required (can be optimized later)
        for (int i = 0; i < 3; i++) {
            t.Balance(dts, {20000 * mErlang, 2500 * mErlang, 5500 * mErlang, 8500 * mErlang});
            t.ApplyMovements();
        }
        t.AssertPoolsCurrentCpus({2, 3, 6, 9});
    }
}