aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/vespa/storageframework/generic/thread/tickingthread.cpp
blob: 35d0f44010b42ab50d7058847a6a7d2e02f036f7 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "tickingthread.h"
#include "threadpool.h"
#include "runnable.h"
#include "thread.h"
#include <vespa/vespalib/stllike/asciistream.h>
#include <cassert>

namespace storage::framework {

ThreadWaitInfo ThreadWaitInfo::MORE_WORK_ENQUEUED(false);
ThreadWaitInfo ThreadWaitInfo::NO_MORE_CRITICAL_WORK_KNOWN(true);

void
ThreadWaitInfo::merge(const ThreadWaitInfo& other) {
    if (!other._waitWanted) {
        _waitWanted = false;
    }
}

/**
 * \brief Implementation actually doing lock handling, waiting, and allowing a
 *        global synchronization point where no thread is currently running.
 */
class TickingThreadRunner final : public Runnable {
    std::mutex              & _monitor;
    std::condition_variable & _cond;
    TickingThread           & _tickingThread;
    uint32_t                  _threadIndex;
    bool                      _wantToFreeze;
    bool                      _frozen;
    char                      _state;

public:
    using SP = std::shared_ptr<TickingThreadRunner>;

    TickingThreadRunner(std::mutex& m,
                        std::condition_variable & cond,
                        TickingThread& ticker,
                        uint32_t threadIndex) noexcept
        : _monitor(m), _cond(cond), _tickingThread(ticker),
          _threadIndex(threadIndex), _wantToFreeze(false), _frozen(false), _state('n') {}

    /**
     * Call to freeze this thread. Returns then the thread has done executing
     * tick and has frozen.
     */
    void freeze() {
        std::unique_lock guard(_monitor);
        _wantToFreeze = true;
        while (!_frozen) {
            _cond.wait(guard);
        }
    }

    /**
     * Call to thaw up a frozen thread so it can continue.
     */
    void thaw() {
        {
            std::lock_guard guard(_monitor);
            _wantToFreeze = false;
        }
        _cond.notify_all();
    }

    [[nodiscard]] char getState() const { return _state; }

private:
    void run(ThreadHandle& handle) override {
        ThreadWaitInfo info = ThreadWaitInfo::MORE_WORK_ENQUEUED;
        CycleType cycle = PROCESS_CYCLE;
        int ticksExecutedAfterWait = 0;
        while (!handle.interrupted()) {
            {
                std::unique_lock guard(_monitor);
                if (info.waitWanted()) {
                    _state = 'w';
                    cycle = WAIT_CYCLE;
                    if (ticksExecutedAfterWait >= handle.getTicksBeforeWait()) {
                        _cond.wait_for(guard, handle.getWaitTime());
                        ticksExecutedAfterWait = 0;
                    }
                }
                if (_wantToFreeze) {
                    _state = 'f';
                    doFreeze(guard, _cond);
                    ticksExecutedAfterWait = 0;
                }
                _state = 'c';
                info.merge(_tickingThread.doCriticalTick(_threadIndex));
                _state = 'n';
            }
            handle.registerTick(cycle);
            ticksExecutedAfterWait++;
            cycle = PROCESS_CYCLE;
            info = _tickingThread.doNonCriticalTick(_threadIndex);
        }
        _state = 's';
    }
    void doFreeze(std::unique_lock<std::mutex> & guard, std::condition_variable & cond) {
        _frozen = true;
        cond.notify_all();
        while (_wantToFreeze) {
            _cond.wait(guard);
        }
        _frozen = false;
    }
};

class TickingThreadPoolImpl final : public TickingThreadPool {
    const vespalib::string               _name;
    const vespalib::duration             _waitTime;
    const vespalib::duration             _maxProcessTime;
    const uint32_t                       _ticksBeforeWait;
    std::mutex                           _lock;
    std::condition_variable              _cond;
    std::vector<TickingThreadRunner::SP> _tickers;
    std::vector<std::shared_ptr<Thread>> _threads;

    struct FreezeGuard final : public TickingLockGuard::Impl {
        TickingThreadPoolImpl& _pool;

        explicit FreezeGuard(TickingThreadPoolImpl& pool) : _pool(pool) { _pool.freeze(); }
        ~FreezeGuard() override { _pool.thaw(); }
        void broadcast() override {}
    };
    struct CriticalGuard final : public TickingLockGuard::Impl {
        std::unique_lock<std::mutex> _guard;
        std::condition_variable &_cond;

        explicit CriticalGuard(std::mutex & lock, std::condition_variable & cond) : _guard(lock), _cond(cond) {}

        void broadcast() override { _cond.notify_all(); }
    };

public:
    TickingThreadPoolImpl(vespalib::stringref name, vespalib::duration waitTime,
                          int ticksBeforeWait, vespalib::duration maxProcessTime)
        : _name(name),
          _waitTime(waitTime),
          _maxProcessTime(maxProcessTime),
          _ticksBeforeWait(ticksBeforeWait)
    { }

    ~TickingThreadPoolImpl() override {
        stop();
    }

    void addThread(TickingThread& ticker) override {
        ThreadIndex index = _tickers.size();
        ticker.newThreadCreated(index);
        _tickers.emplace_back(std::make_shared<TickingThreadRunner>(_lock, _cond, ticker, index));
    }

    void start(ThreadPool& pool) override {
        assert(!_tickers.empty());
        for (uint32_t i=0; i<_tickers.size(); ++i) {
            vespalib::asciistream ost;
            ost << _name.c_str() << " thread " << i;
            _threads.push_back(std::shared_ptr<Thread>(pool.startThread(
                    *_tickers[i],
                    ost.str(),
                    _waitTime,
                    _maxProcessTime,
                    _ticksBeforeWait, std::nullopt)));
        }
    }

    TickingLockGuard freezeAllTicks() override {
        return TickingLockGuard(std::make_unique<FreezeGuard>(*this));
    }

    TickingLockGuard freezeCriticalTicks() override {
        return TickingLockGuard(std::make_unique<CriticalGuard>(_lock, _cond));
    }

    void stop() override {
        for (auto& t : _threads) {
            t->interrupt();
        }
        {
            _cond.notify_all();
        }
        for (auto& t : _threads) {
            t->join();
        }
    }

    vespalib::string getStatus() override {
        vespalib::string result(_tickers.size(), ' ');
        for (uint32_t i=0, n=_tickers.size(); i<n; ++i) {
            result[i] = _tickers[i]->getState();
        }
        return result;
    }

private:
    void freeze() {
        for (auto& t : _tickers) {
            t->freeze();
        }
    }

    void thaw() {
        for (auto& t : _tickers) {
            t->thaw();
        }
    }
};

TickingThreadPool::UP
TickingThreadPool::createDefault(
        vespalib::stringref name,
        vespalib::duration waitTime,
        int ticksBeforeWait,
        vespalib::duration maxProcessTime)
{
    return std::make_unique<TickingThreadPoolImpl>(name, waitTime, ticksBeforeWait, maxProcessTime);
}

TickingThreadPool::UP
TickingThreadPool::createDefault(vespalib::stringref name, vespalib::duration waitTime)
{
    return createDefault(name, waitTime, 1, 5s);
}

} // storage::framework