aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/vespa/storageframework/defaultimplementation/thread/threadimpl.cpp
blob: 5a24549c093041a48a59c909a288459ef9006beb (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "threadimpl.h"
#include "threadpoolimpl.h"
#include <vespa/storageframework/generic/clock/clock.h>
#include <vespa/vespalib/util/atomic.h>
#include <vespa/vespalib/util/signalhandler.h>
#include <cinttypes>

#include <vespa/log/bufferedlogger.h>
LOG_SETUP(".framework.thread.impl");

using namespace vespalib::atomic;

namespace storage::framework::defaultimplementation {

ThreadImpl::ThreadImpl(ThreadPoolImpl& pool,
                       Runnable& runnable,
                       vespalib::stringref id,
                       vespalib::duration waitTime,
                       vespalib::duration maxProcessTime,
                       int ticksBeforeWait,
                       std::optional<vespalib::CpuUsage::Category> cpu_category)
    : Thread(id),
      _pool(pool),
      _runnable(runnable),
      _properties(waitTime, maxProcessTime, ticksBeforeWait),
      _tickData(),
      _tickDataPtr(0),
      _interrupted(false),
      _joined(false),
      _thread(),
      _cpu_category(cpu_category)
{
    _tickData[load_relaxed(_tickDataPtr)]._lastTick = pool.getClock().getMonotonicTime();
    _thread = std::thread([this](){run();});
}

ThreadImpl::~ThreadImpl()
{
    interrupt();
    join();
}

void
ThreadImpl::run()
{
    if (_cpu_category.has_value()) {
        auto usage = vespalib::CpuUsage::use(_cpu_category.value());
        _runnable.run(*this);
    } else {
        _runnable.run(*this);
    }
    _pool.unregisterThread(*this);
    _joined = true;
}

bool
ThreadImpl::interrupted() const
{
    return _interrupted.load(std::memory_order_relaxed);
}

bool
ThreadImpl::joined() const
{
    return _joined;
}

void
ThreadImpl::interrupt()
{
    _interrupted.store(true, std::memory_order_relaxed);
}

void
ThreadImpl::join()
{
    if (_thread.joinable()) {
        _thread.join();
    }
}

vespalib::string
ThreadImpl::get_live_thread_stack_trace() const
{
    auto native_handle = const_cast<std::thread&>(_thread).native_handle();
    return vespalib::SignalHandler::get_cross_thread_stack_trace(native_handle);
}

void
ThreadImpl::registerTick(CycleType cycleType) {
    registerTick(cycleType, _pool.getClock().getMonotonicTime());
}

void
ThreadImpl::registerTick(CycleType cycleType, vespalib::steady_time now)
{
    if (now.time_since_epoch() == vespalib::duration::zero()) now = _pool.getClock().getMonotonicTime();
    ThreadTickData data(getTickData());
    vespalib::steady_clock::time_point previousTick = data._lastTick;
    data._lastTick = now;
    data._lastTickType = cycleType;
    setTickData(data);

    if (data._lastTick.time_since_epoch() == vespalib::duration::zero()) { return; }

    if (previousTick > now) {
        LOGBP(warning, "Thread is registering tick at time %" PRIu64 ", but "
                       "last time it registered a tick, the time was %" PRIu64
                       ". Assuming clock has been adjusted backwards",
	      vespalib::count_ms(now.time_since_epoch()), vespalib::count_ms(previousTick.time_since_epoch()));
        return;
    }
    vespalib::duration cycleTime = now - previousTick;
    if (cycleType == WAIT_CYCLE) {
        data._maxWaitTimeSeen = std::max(data._maxWaitTimeSeen, cycleTime);
    } else {
        data._maxProcessingTimeSeen = std::max(data._maxProcessingTimeSeen, cycleTime);
    }
}

ThreadTickData
ThreadImpl::getTickData() const
{
    return _tickData[load_acquire(_tickDataPtr)].loadRelaxed();
}

void
ThreadImpl::setTickData(const ThreadTickData& tickData)
{
    uint32_t nextData = (load_relaxed(_tickDataPtr) + 1) % _tickData.size();
    _tickData[nextData].storeRelaxed(tickData);
    store_release(_tickDataPtr, nextData);
}

ThreadTickData
ThreadImpl::AtomicThreadTickData::loadRelaxed() const noexcept
{
    ThreadTickData result;
    constexpr auto relaxed = std::memory_order_relaxed;
    result._lastTickType = _lastTickType.load(relaxed);
    result._lastTick = _lastTick.load(relaxed);
    result._maxProcessingTimeSeen = _maxProcessingTimeSeen.load(relaxed);
    result._maxWaitTimeSeen = _maxWaitTimeSeen.load(relaxed);
    return result;
}

void
ThreadImpl::AtomicThreadTickData::storeRelaxed(const ThreadTickData& newState) noexcept
{
    constexpr auto relaxed = std::memory_order_relaxed;
    _lastTickType.store(newState._lastTickType, relaxed);
    _lastTick.store(newState._lastTick, relaxed);
    _maxProcessingTimeSeen.store(newState._maxProcessingTimeSeen, relaxed);
    _maxWaitTimeSeen.store(newState._maxWaitTimeSeen, relaxed);
}

}