aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/core/scheduler_basic.cpp
blob: 020f640ac0c6dc4aff633831fa823b6a83d802c9 (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
#include "scheduler_basic.h"
#include "scheduler_queue.h"
#include "actor.h"

#include <library/cpp/actors/util/datetime.h>
#include <library/cpp/actors/util/thread.h>

#ifdef BALLOC
#include <library/cpp/balloc/optional/operators.h>
#endif

namespace NActors {

    struct TBasicSchedulerThread::TMonCounters {
        NMonitoring::TDynamicCounters::TCounterPtr TimeDelayMs;
        NMonitoring::TDynamicCounters::TCounterPtr QueueSize;
        NMonitoring::TDynamicCounters::TCounterPtr EventsSent;
        NMonitoring::TDynamicCounters::TCounterPtr EventsDropped;
        NMonitoring::TDynamicCounters::TCounterPtr EventsAdded;
        NMonitoring::TDynamicCounters::TCounterPtr Iterations;
        NMonitoring::TDynamicCounters::TCounterPtr Sleeps;
        NMonitoring::TDynamicCounters::TCounterPtr ElapsedMicrosec;

        TMonCounters(const NMonitoring::TDynamicCounterPtr& counters)
            : TimeDelayMs(counters->GetCounter("Scheduler/TimeDelayMs", false))
            , QueueSize(counters->GetCounter("Scheduler/QueueSize", false))
            , EventsSent(counters->GetCounter("Scheduler/EventsSent", true))
            , EventsDropped(counters->GetCounter("Scheduler/EventsDropped", true))
            , EventsAdded(counters->GetCounter("Scheduler/EventsAdded", true))
            , Iterations(counters->GetCounter("Scheduler/Iterations", true))
            , Sleeps(counters->GetCounter("Scheduler/Sleeps", true))
            , ElapsedMicrosec(counters->GetCounter("Scheduler/ElapsedMicrosec", true))
        { }
    };

    TBasicSchedulerThread::TBasicSchedulerThread(const TSchedulerConfig& config)
        : Config(config)
        , MonCounters(Config.MonCounters ? new TMonCounters(Config.MonCounters) : nullptr)
        , ActorSystem(nullptr)
        , CurrentTimestamp(nullptr)
        , CurrentMonotonic(nullptr)
        , TotalReaders(0)
        , StopFlag(false)
        , ScheduleMap(3600)
    {
        Y_VERIFY(!Config.UseSchedulerActor, "Cannot create scheduler thread because Config.UseSchedulerActor# true");
    }

    TBasicSchedulerThread::~TBasicSchedulerThread() {
        Y_VERIFY(!MainCycle);
    }

    void TBasicSchedulerThread::CycleFunc() {
#ifdef BALLOC
        ThreadDisableBalloc();
#endif
        ::SetCurrentThreadName("Scheduler");

        ui64 currentMonotonic = RelaxedLoad(CurrentMonotonic);
        ui64 throttledMonotonic = currentMonotonic;

        ui64 activeTick = AlignUp<ui64>(throttledMonotonic, IntrasecondThreshold);
        TAutoPtr<TMomentMap> activeSec;

        NHPTimer::STime hpprev = GetCycleCountFast();
        ui64 nextTimestamp = TInstant::Now().MicroSeconds();
        ui64 nextMonotonic = Max(currentMonotonic, GetMonotonicMicroSeconds());

        while (!AtomicLoad(&StopFlag)) {
            {
                const ui64 delta = nextMonotonic - throttledMonotonic;
                const ui64 elapsedDelta = nextMonotonic - currentMonotonic;
                const ui64 threshold = Max(Min(Config.ProgressThreshold, 2 * elapsedDelta), ui64(1));

                throttledMonotonic = (delta > threshold) ? throttledMonotonic + threshold : nextMonotonic;

                if (MonCounters) {
                    *MonCounters->TimeDelayMs = (nextMonotonic - throttledMonotonic) / 1000;
                }
            }
            AtomicStore(CurrentTimestamp, nextTimestamp);
            AtomicStore(CurrentMonotonic, nextMonotonic);
            currentMonotonic = nextMonotonic;

            if (MonCounters) {
                ++*MonCounters->Iterations;
            }

            bool somethingDone = false;

            // first step - send everything triggered on schedule
            ui64 eventsSent = 0;
            ui64 eventsDropped = 0;
            for (;;) {
                while (!!activeSec && !activeSec->empty()) {
                    TMomentMap::iterator it = activeSec->begin();
                    if (it->first <= throttledMonotonic) {
                        if (NSchedulerQueue::TQueueType* q = it->second.Get()) {
                            while (NSchedulerQueue::TEntry* x = q->Reader.Pop()) {
                                somethingDone = true;
                                Y_VERIFY_DEBUG(x->InstantMicroseconds <= activeTick);
                                IEventHandle* ev = x->Ev;
                                ISchedulerCookie* cookie = x->Cookie;
                                // TODO: lazy send with backoff queue to not hang over contended mailboxes
                                if (cookie) {
                                    if (cookie->Detach()) {
                                        ActorSystem->Send(ev);
                                        ++eventsSent;
                                    } else {
                                        delete ev;
                                        ++eventsDropped;
                                    }
                                } else {
                                    ActorSystem->Send(ev);
                                    ++eventsSent;
                                }
                            }
                        }
                        activeSec->erase(it);
                    } else
                        break;
                }

                if (activeTick <= throttledMonotonic) {
                    Y_VERIFY_DEBUG(!activeSec || activeSec->empty());
                    activeSec.Destroy();
                    activeTick += IntrasecondThreshold;
                    TScheduleMap::iterator it = ScheduleMap.find(activeTick);
                    if (it != ScheduleMap.end()) {
                        activeSec = it->second;
                        ScheduleMap.erase(it);
                    }
                    continue;
                }

                // ok, if we are here - then nothing is ready, so send step complete
                break;
            }

            // second step - collect everything from queues

            ui64 eventsAdded = 0;
            for (ui32 i = 0; i != TotalReaders; ++i) {
                while (NSchedulerQueue::TEntry* x = Readers[i]->Pop()) {
                    somethingDone = true;
                    const ui64 instant = AlignUp<ui64>(x->InstantMicroseconds, Config.ResolutionMicroseconds);
                    IEventHandle* const ev = x->Ev;
                    ISchedulerCookie* const cookie = x->Cookie;

                    // check is cookie still valid? looks like it will hurt performance w/o sagnificant memory save

                    if (instant <= activeTick) {
                        if (!activeSec)
                            activeSec.Reset(new TMomentMap());
                        TAutoPtr<NSchedulerQueue::TQueueType>& queue = (*activeSec)[instant];
                        if (!queue)
                            queue.Reset(new NSchedulerQueue::TQueueType());
                        queue->Writer.Push(instant, ev, cookie);
                    } else {
                        const ui64 intrasecond = AlignUp<ui64>(instant, IntrasecondThreshold);
                        TAutoPtr<TMomentMap>& msec = ScheduleMap[intrasecond];
                        if (!msec)
                            msec.Reset(new TMomentMap());
                        TAutoPtr<NSchedulerQueue::TQueueType>& queue = (*msec)[instant];
                        if (!queue)
                            queue.Reset(new NSchedulerQueue::TQueueType());
                        queue->Writer.Push(instant, ev, cookie);
                    }

                    ++eventsAdded;
                }
            }

            NHPTimer::STime hpnow = GetCycleCountFast();

            if (MonCounters) {
                *MonCounters->QueueSize -= eventsSent + eventsDropped;
                *MonCounters->QueueSize += eventsAdded;
                *MonCounters->EventsSent += eventsSent;
                *MonCounters->EventsDropped += eventsDropped;
                *MonCounters->EventsAdded += eventsAdded;
                *MonCounters->ElapsedMicrosec += NHPTimer::GetSeconds(hpnow - hpprev) * 1000000;
            }

            hpprev = hpnow;
            nextTimestamp = TInstant::Now().MicroSeconds();
            nextMonotonic = Max(currentMonotonic, GetMonotonicMicroSeconds());

            // ok complete, if nothing left - sleep
            if (!somethingDone) {
                const ui64 nextInstant = AlignDown<ui64>(throttledMonotonic + Config.ResolutionMicroseconds, Config.ResolutionMicroseconds);
                if (nextMonotonic >= nextInstant) // already in next time-slice
                    continue;

                const ui64 delta = nextInstant - nextMonotonic;
                if (delta < Config.SpinThreshold) // not so much time left, just spin
                    continue;

                if (MonCounters) {
                    ++*MonCounters->Sleeps;
                }

                NanoSleep(delta * 1000); // ok, looks like we should sleep a bit.

                // Don't count sleep in elapsed microseconds
                hpprev = GetCycleCountFast();
                nextTimestamp = TInstant::Now().MicroSeconds();
                nextMonotonic = Max(currentMonotonic, GetMonotonicMicroSeconds());
            }
        }
        // ok, die!
    }

    void TBasicSchedulerThread::Prepare(TActorSystem* actorSystem, volatile ui64* currentTimestamp, volatile ui64* currentMonotonic) {
        ActorSystem = actorSystem;
        CurrentTimestamp = currentTimestamp;
        CurrentMonotonic = currentMonotonic;
        *CurrentTimestamp = TInstant::Now().MicroSeconds();
        *CurrentMonotonic = GetMonotonicMicroSeconds();
    }

    void TBasicSchedulerThread::PrepareSchedules(NSchedulerQueue::TReader** readers, ui32 scheduleReadersCount) {
        Y_VERIFY(scheduleReadersCount > 0);
        TotalReaders = scheduleReadersCount;
        Readers.Reset(new NSchedulerQueue::TReader*[scheduleReadersCount]);
        Copy(readers, readers + scheduleReadersCount, Readers.Get());
    }

    void TBasicSchedulerThread::PrepareStart() {
        // Called after actor system is initialized, but before executor threads
        // are started, giving us a chance to update current timestamp with a
        // more recent value, taking initialization time into account. This is
        // safe to do, since scheduler thread is not started yet, so no other
        // threads are updating time concurrently.
        AtomicStore(CurrentTimestamp, TInstant::Now().MicroSeconds());
        AtomicStore(CurrentMonotonic, Max(RelaxedLoad(CurrentMonotonic), GetMonotonicMicroSeconds()));
    }

    void TBasicSchedulerThread::Start() {
        MainCycle.Reset(new NThreading::TLegacyFuture<void, false>(std::bind(&TBasicSchedulerThread::CycleFunc, this)));
    }

    void TBasicSchedulerThread::PrepareStop() {
        AtomicStore(&StopFlag, true);
    }

    void TBasicSchedulerThread::Stop() {
        MainCycle->Get();
        MainCycle.Destroy();
    }

}

#ifdef __linux__

namespace NActors {
    ISchedulerThread* CreateSchedulerThread(const TSchedulerConfig& config) {
        if (config.UseSchedulerActor) {
            return new TMockSchedulerThread();
        } else {
            return new TBasicSchedulerThread(config);
        }
    }

}

#else //  __linux__

namespace NActors {
    ISchedulerThread* CreateSchedulerThread(const TSchedulerConfig& config) {
        return new TBasicSchedulerThread(config);
    }
}

#endif // __linux__