aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/util/cpu_usage.cpp
blob: 89ba03fdab9439034bac4fcdc10ed1244e42977d (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "cpu_usage.h"
#include "require.h"
#include <pthread.h>
#include <optional>
#include <cassert>

#include <sys/resource.h>

namespace vespalib {

namespace cpu_usage {

namespace {

class DummyThreadSampler : public ThreadSampler {
private:
    steady_time _start;
    double _util;
public:
    DummyThreadSampler(double util) : _start(steady_clock::now()), _util(util) {}
    duration sample() const noexcept override {
        return from_s(to_s(steady_clock::now() - _start) * _util);
    }
};

#ifdef __linux__

class LinuxThreadSampler : public ThreadSampler {
private:
    clockid_t _my_clock;
public:
    LinuxThreadSampler() : _my_clock() {
        REQUIRE_EQ(pthread_getcpuclockid(pthread_self(), &_my_clock), 0);
    }
    duration sample() const noexcept override {
        timespec ts;
        memset(&ts, 0, sizeof(ts));
        clock_gettime(_my_clock, &ts);
        return from_timespec(ts);
    }
};

#endif

} // <unnamed>

duration total_cpu_usage() noexcept {
        timespec ts;
        memset(&ts, 0, sizeof(ts));
        clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
        return from_timespec(ts);
}

ThreadSampler::UP create_thread_sampler(bool force_mock_impl, double expected_util) {
    if (force_mock_impl) {
        return std::make_unique<DummyThreadSampler>(expected_util);
    }
#ifdef __linux__
    return std::make_unique<LinuxThreadSampler>();
#endif
    return std::make_unique<DummyThreadSampler>(expected_util);
}

} // cpu_usage

CpuUsage::ThreadTrackerImpl::ThreadTrackerImpl(cpu_usage::ThreadSampler::UP sampler)
  : _lock(),
    _cat(Category::OTHER),
    _old_usage(),
    _sampler(std::move(sampler)),
    _pending()
{
}

CpuUsage::Category
CpuUsage::ThreadTrackerImpl::set_category(Category new_cat) noexcept
{
    // only owning thread may change category
    if (new_cat == _cat) {
        return new_cat;
    }
    Guard guard(_lock);
    duration new_usage = _sampler->sample();
    if (_cat != Category::OTHER) {
        _pending[_cat] += (new_usage - _old_usage);
    }
    _old_usage = new_usage;
    auto old_cat = _cat;
    _cat = new_cat;
    return old_cat;
}

CpuUsage::Sample
CpuUsage::ThreadTrackerImpl::sample() noexcept
{
    Guard guard(_lock);
    if (_cat != Category::OTHER) {
        duration new_usage = _sampler->sample();
        _pending[_cat] += (new_usage - _old_usage);
        _old_usage = new_usage;
    }
    Sample sample = _pending;
    _pending = Sample();
    return sample;
}

vespalib::string &
CpuUsage::name_of(Category cat)
{
    static std::array<vespalib::string,num_categories> names = {"setup", "read", "write", "compact", "other"};
    return names[index_of(cat)];
}

CpuUsage::Category
CpuUsage::MyUsage::set_cpu_category_for_this_thread(Category cat) noexcept
{
    struct Wrapper {
        std::shared_ptr<ThreadTrackerImpl> self;
        Wrapper() : self(std::make_shared<ThreadTrackerImpl>(cpu_usage::create_thread_sampler())) {
            CpuUsage::self().add_thread(self);
        }
        ~Wrapper() {
            self->set_category(CpuUsage::Category::OTHER);
            CpuUsage::self().remove_thread(std::move(self));
        }
    };
    thread_local Wrapper wrapper;
    return wrapper.self->set_category(cat);
}

CpuUsage::CpuUsage()
  : _lock(),
    _usage(),
    _threads(),
    _sampling(false),
    _conflict(),
    _pending_add(),
    _pending_remove()
{
}

CpuUsage::~CpuUsage() = default;

CpuUsage &
CpuUsage::self()
{
    static CpuUsage me;
    return me;
}

void
CpuUsage::do_add_thread(const Guard &, ThreadTracker::SP tracker)
{
    assert(!_sampling);
    auto *key = tracker.get();
    auto [ignore, was_inserted] = _threads.emplace(key, std::move(tracker));
    assert(was_inserted);
}

void
CpuUsage::do_remove_thread(const Guard &, ThreadTracker::SP tracker)
{
    assert(!_sampling);
    _usage.merge(tracker->sample());
    auto was_removed = _threads.erase(tracker.get());
    assert(was_removed);
}

void
CpuUsage::add_thread(ThreadTracker::SP tracker)
{
    Guard guard(_lock);
    if (_sampling) {
        _pending_add.push_back(std::move(tracker));
    } else {
        do_add_thread(guard, std::move(tracker));
    }
}

void
CpuUsage::remove_thread(ThreadTracker::SP tracker)
{
    Guard guard(_lock);
    if (_sampling) {
        _pending_remove.push_back(std::move(tracker));
    } else {
        do_remove_thread(guard, std::move(tracker));
    }
}

void
CpuUsage::handle_pending(const Guard &guard)
{
    for (auto &thread: _pending_add) {
        do_add_thread(guard, std::move(thread));
    }
    _pending_add.clear();
    for (auto &thread: _pending_remove) {
        do_remove_thread(guard, std::move(thread));
    }
    _pending_remove.clear();
}

CpuUsage::TimedSample
CpuUsage::do_sample()
{
    assert(_sampling);
    Sample my_sample;
    std::optional<std::promise<TimedSample>> my_promise;
    auto t = steady_clock::now();
    for (const auto &entry: _threads) {
        my_sample.merge(entry.first->sample());
    }
    {
        Guard guard(_lock);
        _sampling = false;
        handle_pending(guard);
        if (_conflict) {
            my_promise = std::move(_conflict->sample_promise);
            _conflict.reset();
        }
        my_sample.merge(_usage);
        _usage = my_sample;
    }
    auto total = cpu_usage::total_cpu_usage();
    for (size_t i = 0; i < index_of(Category::OTHER); ++i) {
        total -= my_sample[i];
    }
    my_sample[Category::OTHER] = std::max(total, duration::zero());
    TimedSample result{t, my_sample};
    if (my_promise.has_value()) {
        my_promise.value().set_value(result);
    }
    return result;
}

CpuUsage::TimedSample
CpuUsage::sample_or_wait()
{
    std::shared_future<TimedSample> my_future;
    {
        Guard guard(_lock);
        if (_sampling) {
            if (!_conflict) {
                _conflict = std::make_unique<SampleConflict>();
            }
            my_future = _conflict->future_sample;
            _conflict->waiters++;
        } else {
            _sampling = true;
        }
    }
    if (my_future.valid()) {
        return my_future.get();
    } else {
        return do_sample();
    }
}

CpuUsage::TimedSample
CpuUsage::sample()
{
    return self().sample_or_wait();
}

Runnable::init_fun_t
CpuUsage::wrap(Runnable::init_fun_t init, Category cat)
{
    return [init,cat](Runnable &target) {
        auto my_usage = CpuUsage::use(cat);
        return init(target);
    };
}

Executor::Task::UP
CpuUsage::wrap(Executor::Task::UP task, Category cat)
{
    struct CpuTask : Executor::Task {
        UP task;
        Category cat;
        CpuTask(UP task_in, Category cat_in)
          : task(std::move(task_in)), cat(cat_in) {}
        void run() override {
            auto my_usage = CpuUsage::use(cat);
            task->run();
        }
    };
    return std::make_unique<CpuTask>(std::move(task), cat);
}

} // namespace