aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/util/thread_load_log.h
blob: b4b34d47bb5cd1184ecf6b923ab3e0dca85ed8d9 (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
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
#pragma once

#include "defs.h"

#include <util/system/types.h>

#include <type_traits>
#include <algorithm>
#include <atomic>
#include <limits>
#include <queue>

template <ui64 TIME_SLOT_COUNT, ui64 TIME_SLOT_LENGTH_NS = 131'072, typename Type = std::uint8_t>
class TThreadLoad {
public:
    using TimeSlotType = Type;

private:
    static constexpr auto TIME_SLOT_MAX_VALUE = std::numeric_limits<TimeSlotType>::max();
    static constexpr ui64 TIME_SLOT_PART_COUNT = TIME_SLOT_MAX_VALUE + 1;
    static constexpr auto TIME_SLOT_PART_LENGTH_NS = TIME_SLOT_LENGTH_NS / TIME_SLOT_PART_COUNT;

    template <typename T>
    static void AtomicAddBound(std::atomic<T>& val, i64 inc) {
        if (inc == 0) {
            return;
        }

        auto newVal = val.load();
        auto oldVal = newVal;

        do {
            static constexpr auto MAX_VALUE = std::numeric_limits<T>::max();

            if (oldVal >= MAX_VALUE) {
                return;
            }
            newVal = std::min<i64>(MAX_VALUE, static_cast<i64>(oldVal) + inc);
        } while (!val.compare_exchange_weak(oldVal, newVal));
    }

    template <typename T>
    static void AtomicSubBound(std::atomic<T>& val, i64 sub) {
        if (sub == 0) {
            return;
        }

        auto newVal = val.load();
        auto oldVal = newVal;

        do {
            if (oldVal == 0) {
                return;
            }
            newVal = std::max<i64>(0, static_cast<i64>(oldVal) - sub);
        } while (!val.compare_exchange_weak(oldVal, newVal));
    }

    void UpdateCompleteTimeSlots(ui64 firstSlotNumber, ui64 lastSlotNumber, TimeSlotType timeSlotValue) {
        ui32 firstSlotIndex = firstSlotNumber % TIME_SLOT_COUNT;
        ui32 lastSlotIndex = lastSlotNumber % TIME_SLOT_COUNT;

        const ui64 firstTimeSlotsPass = firstSlotNumber / TIME_SLOT_COUNT;
        const ui64 lastTimeSlotsPass = lastSlotNumber / TIME_SLOT_COUNT;

        if (firstTimeSlotsPass == lastTimeSlotsPass) {
            // first and last time slots are in the same pass
            for (auto slotNumber = firstSlotNumber + 1; slotNumber < lastSlotNumber; ++slotNumber) {
                auto slotIndex = slotNumber % TIME_SLOT_COUNT;
                TimeSlots[slotIndex] = timeSlotValue;
            }
        } else if (firstTimeSlotsPass + 1 == lastTimeSlotsPass) {
            for (auto slotIndex = (firstSlotNumber + 1) % TIME_SLOT_COUNT; firstSlotIndex < slotIndex && slotIndex < TIME_SLOT_COUNT; ++slotIndex) {
                TimeSlots[slotIndex] = timeSlotValue;
            }
            for (auto slotIndex = 0u; slotIndex < lastSlotIndex; ++slotIndex) {
                TimeSlots[slotIndex] = timeSlotValue;
            }
        } else {
            for (auto slotIndex = 0u; slotIndex < TIME_SLOT_COUNT; ++slotIndex) {
                TimeSlots[slotIndex] = timeSlotValue;
            }
        }
    }

public:
    std::atomic<ui64> LastTimeNs;
    std::atomic<TimeSlotType> TimeSlots[TIME_SLOT_COUNT];
    std::atomic<bool> LastRegisteredPeriodIsBusy = false;

    explicit TThreadLoad(ui64 timeNs = 0) {
        static_assert(std::is_unsigned<TimeSlotType>::value);

        LastTimeNs = timeNs;
        for (size_t i = 0; i < TIME_SLOT_COUNT; ++i) {
            TimeSlots[i] = 0;
        }
    }

    static constexpr auto GetTimeSlotCount() {
        return TIME_SLOT_COUNT;
    }

    static constexpr auto GetTimeSlotLengthNs() {
        return TIME_SLOT_LENGTH_NS;
    }

    static constexpr auto GetTimeSlotPartLengthNs() {
        return TIME_SLOT_PART_LENGTH_NS;
    }

    static constexpr auto GetTimeSlotPartCount() {
        return TIME_SLOT_PART_COUNT;
    }

    static constexpr auto GetTimeSlotMaxValue() {
        return TIME_SLOT_MAX_VALUE;
    }

    static constexpr auto GetTimeWindowLengthNs() {
        return TIME_SLOT_COUNT * TIME_SLOT_LENGTH_NS;
    }

    void RegisterBusyPeriod(ui64 timeNs) {
        RegisterBusyPeriod<true>(timeNs, LastTimeNs.load());
    }

    template <bool ModifyLastTime>
    void RegisterBusyPeriod(ui64 timeNs, ui64 lastTimeNs) {
        LastRegisteredPeriodIsBusy = true;

        if (timeNs < lastTimeNs) {
            // when time goes back, mark all time slots as 'free'
            for (size_t i = 0u; i < TIME_SLOT_COUNT; ++i) {
                TimeSlots[i] = 0;
            }

            if (ModifyLastTime) {
                LastTimeNs = timeNs;
            }

            return;
        }

        // lastTimeNs <= timeNs
        ui64 firstSlotNumber = lastTimeNs / TIME_SLOT_LENGTH_NS;
        ui32 firstSlotIndex = firstSlotNumber % TIME_SLOT_COUNT;
        ui64 lastSlotNumber = timeNs / TIME_SLOT_LENGTH_NS;
        ui32 lastSlotIndex = lastSlotNumber % TIME_SLOT_COUNT;

        if (firstSlotNumber == lastSlotNumber) {
            ui32 slotLengthNs = timeNs - lastTimeNs;
            ui32 slotPartsCount = (slotLengthNs + TIME_SLOT_PART_LENGTH_NS - 1) / TIME_SLOT_PART_LENGTH_NS;
            AtomicAddBound(TimeSlots[firstSlotIndex], slotPartsCount);

            if (ModifyLastTime) {
                LastTimeNs = timeNs;
            }
            return;
        }

        ui32 firstSlotLengthNs = TIME_SLOT_LENGTH_NS - (lastTimeNs % TIME_SLOT_LENGTH_NS);
        ui32 firstSlotPartsCount = (firstSlotLengthNs + TIME_SLOT_PART_LENGTH_NS - 1) / TIME_SLOT_PART_LENGTH_NS;
        ui32 lastSlotLengthNs = timeNs % TIME_SLOT_LENGTH_NS;
        ui32 lastSlotPartsCount = (lastSlotLengthNs + TIME_SLOT_PART_LENGTH_NS - 1) / TIME_SLOT_PART_LENGTH_NS;

        // process first time slot
        AtomicAddBound(TimeSlots[firstSlotIndex], firstSlotPartsCount);

        // process complete time slots
        UpdateCompleteTimeSlots(firstSlotNumber, lastSlotNumber, TIME_SLOT_MAX_VALUE);

        // process last time slot
        AtomicAddBound(TimeSlots[lastSlotIndex], lastSlotPartsCount);

        if (ModifyLastTime) {
            LastTimeNs = timeNs;
        }
    }

    void RegisterIdlePeriod(ui64 timeNs) {
        LastRegisteredPeriodIsBusy = false;

        ui64 lastTimeNs = LastTimeNs.load();
        if (timeNs < lastTimeNs) {
            // when time goes back, mark all time slots as 'busy'
            for (size_t i = 0u; i < TIME_SLOT_COUNT; ++i) {
                TimeSlots[i] = TIME_SLOT_MAX_VALUE;
            }
            LastTimeNs = timeNs;
            return;
        }

        // lastTimeNs <= timeNs
        ui64 firstSlotNumber = lastTimeNs / TIME_SLOT_LENGTH_NS;
        ui32 firstSlotIndex = firstSlotNumber % TIME_SLOT_COUNT;
        ui64 lastSlotNumber = timeNs / TIME_SLOT_LENGTH_NS;
        ui32 lastSlotIndex = lastSlotNumber % TIME_SLOT_COUNT;

        if (firstSlotNumber == lastSlotNumber) {
            ui32 slotLengthNs = timeNs - lastTimeNs;
            ui32 slotPartsCount = slotLengthNs / TIME_SLOT_PART_LENGTH_NS;

            AtomicSubBound(TimeSlots[firstSlotIndex], slotPartsCount);

            LastTimeNs = timeNs;
            return;
        }

        ui32 firstSlotLengthNs = TIME_SLOT_LENGTH_NS - (lastTimeNs % TIME_SLOT_LENGTH_NS);
        ui32 firstSlotPartsCount = (firstSlotLengthNs + TIME_SLOT_PART_LENGTH_NS - 1) / TIME_SLOT_PART_LENGTH_NS;
        ui32 lastSlotLengthNs = timeNs % TIME_SLOT_LENGTH_NS;
        ui32 lastSlotPartsCount = (lastSlotLengthNs + TIME_SLOT_PART_LENGTH_NS - 1) / TIME_SLOT_PART_LENGTH_NS;

        // process first time slot
        AtomicSubBound(TimeSlots[firstSlotIndex], firstSlotPartsCount);

        // process complete time slots
        UpdateCompleteTimeSlots(firstSlotNumber, lastSlotNumber, 0);

        // process last time slot
        AtomicSubBound(TimeSlots[lastSlotIndex], lastSlotPartsCount);

        LastTimeNs = timeNs;
    }
};

class TMinusOneThreadEstimator {
private:
    template <typename T, int MaxSize>
    class TArrayQueue {
    public:
        bool empty() const {
            return FrontIndex == -1;
        }

        bool full() const {
            return (RearIndex + 1) % MaxSize == FrontIndex;
        }

        T& front() {
            return Data[FrontIndex];
        }

        bool push(T &&t) {
            if (full()) {
                return false;
            }

            if (FrontIndex == -1) {
                FrontIndex = 0;
            }

            RearIndex = (RearIndex + 1) % MaxSize;
            Data[RearIndex] = std::move(t);
            return true;
        }

        bool pop() {
            if (empty()) {
                return false;
            }

            if (FrontIndex == RearIndex) {
                FrontIndex = RearIndex = -1;
            } else {
                FrontIndex = (FrontIndex + 1) % MaxSize;
            }

            return true;
        }

    private:
        int FrontIndex = -1;
        int RearIndex = -1;
        T Data[MaxSize];
    };

public:
    template <typename T>
    ui64 MaxLatencyIncreaseWithOneLessCpu(T **threadLoads, ui32 threadCount, ui64 timeNs, ui64 periodNs) {
        Y_VERIFY(threadCount > 0);

        struct TTimeSlotData {
            typename T::TimeSlotType Load;
            ui64 Index;
        };

        ui64 lastTimeNs = timeNs;
        for (auto threadIndex = 0u; threadIndex < threadCount; ++threadIndex) {
            if (threadLoads[threadIndex]->LastRegisteredPeriodIsBusy.load()) {
                lastTimeNs = std::min(lastTimeNs, threadLoads[threadIndex]->LastTimeNs.load());
            } else {
                // make interval [lastTimeNs, timeNs] 'busy'
                threadLoads[threadIndex]->template RegisterBusyPeriod<false>(timeNs, threadLoads[threadIndex]->LastTimeNs.load());
            }
        }

        periodNs = std::min(T::GetTimeWindowLengthNs(), periodNs);

        ui64 beginTimeNs = periodNs < timeNs ? timeNs - periodNs : 0;

        ui64 firstSlotNumber = beginTimeNs / T::GetTimeSlotLengthNs();
        ui64 lastSlotNumber = (lastTimeNs + T::GetTimeSlotLengthNs() - 1) / T::GetTimeSlotLengthNs();

        ui64 maxTimeSlotShiftCount = 0u;
        TArrayQueue<TTimeSlotData, T::GetTimeSlotCount()> firstThreadLoadDataQueue;

        for (auto slotNumber = firstSlotNumber; slotNumber < lastSlotNumber; ++slotNumber) {
            ui64 slotIndex = slotNumber % T::GetTimeSlotCount();

            typename T::TimeSlotType firstThreadTimeSlotValue = threadLoads[0]->TimeSlots[slotIndex].load();

            // distribute previous load of the first thread by other threads
            auto foundIdleThread = false;

            for (auto threadIndex = 1u; threadIndex < threadCount; ++threadIndex) {
                typename T::TimeSlotType thisThreadAvailableTimeSlotLoad = threadLoads[threadIndex]->GetTimeSlotMaxValue() - threadLoads[threadIndex]->TimeSlots[slotIndex].load();

                while (!firstThreadLoadDataQueue.empty() && thisThreadAvailableTimeSlotLoad > 0) {
                    auto& firstThreadLoadData = firstThreadLoadDataQueue.front();

                    auto distributedLoad = std::min(thisThreadAvailableTimeSlotLoad, firstThreadLoadData.Load);

                    thisThreadAvailableTimeSlotLoad -= distributedLoad;
                    firstThreadLoadData.Load -= distributedLoad;

                    if (firstThreadLoadData.Load == 0) {
                        auto timeSlotShiftCount = slotIndex - firstThreadLoadData.Index;
                        maxTimeSlotShiftCount = std::max(maxTimeSlotShiftCount, timeSlotShiftCount);
                        auto res = firstThreadLoadDataQueue.pop();
                        Y_VERIFY(res);
                    }
                }

                if (thisThreadAvailableTimeSlotLoad == threadLoads[threadIndex]->GetTimeSlotMaxValue()) {
                    foundIdleThread = true;
                }
            }

            // distribute current load of the first thread by other threads
            if (firstThreadTimeSlotValue > 0) {
                if (foundIdleThread) {
                    // The current load of the first thead can be
                    // moved to the idle thread so there is nothing to do
                } else {
                    // The current load of the first thread can be later
                    // processed by the following time slots of other threads
                    auto res = firstThreadLoadDataQueue.push({firstThreadTimeSlotValue, slotIndex});
                    Y_VERIFY(res);
                }
            }
        }

        if (!firstThreadLoadDataQueue.empty()) {
            const auto& timeSlotData = firstThreadLoadDataQueue.front();
            auto timeSlotShiftCount = T::GetTimeSlotCount() - timeSlotData.Index;
            maxTimeSlotShiftCount = std::max(maxTimeSlotShiftCount, timeSlotShiftCount);
        }

        return maxTimeSlotShiftCount * T::GetTimeSlotLengthNs();
    }
};