-
Notifications
You must be signed in to change notification settings - Fork 494
Expand file tree
/
Copy pathDataProcessingStates.cxx
More file actions
249 lines (237 loc) · 10 KB
/
DataProcessingStates.cxx
File metadata and controls
249 lines (237 loc) · 10 KB
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
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#include "Framework/DataProcessingStates.h"
#include "Framework/RuntimeError.h"
#include "Framework/ServiceRegistryRef.h"
#include "Framework/DeviceState.h"
#include "Framework/Logger.h"
#include <uv.h>
#include <iostream>
#include <atomic>
#include <utility>
#include <thread>
#include <string_view>
namespace o2::framework
{
DataProcessingStates::DataProcessingStates(std::function<void(int64_t& base, int64_t& offset)> getRealtimeBase_,
std::function<int64_t(int64_t base, int64_t offset)> getTimestamp_)
: getTimestamp(getTimestamp_),
getRealtimeBase(getRealtimeBase_)
{
getRealtimeBase(realTimeBase, initialTimeOffset);
}
void DataProcessingStates::processCommandQueue()
{
int position = lastInsertedState.load(std::memory_order_relaxed);
// Process the commands in the order we have just computed.
while (position < DataProcessingStates::STATES_BUFFER_SIZE) {
DataProcessingStates::CommandHeader header;
// Avoid alignment issues.
memcpy(&header, &store[position], sizeof(DataProcessingStates::CommandHeader));
int id = header.id;
int64_t timestamp = header.timestamp;
int size = header.size;
assert(id < updateInfos.size());
auto& update = updateInfos[id];
// We need to update only once per invoked callback,
// because the metrics are stored in reverse insertion order.
if (generation > update.generation) {
// If we have enough capacity, we reuse the buffer.
// If the size is the same, we mark it as updated only if different.
if (statesViews[id].size == size) {
int diff = memcmp(statesBuffer.data() + statesViews[id].first, &store[position + sizeof(DataProcessingStates::CommandHeader)], size);
if (diff) {
memcpy(statesBuffer.data() + statesViews[id].first, &store[position + sizeof(DataProcessingStates::CommandHeader)], size);
updated[id] = true;
update.timestamp = timestamp;
update.generation = generation;
pushedMetricsLapse++;
}
} else if (statesViews[id].capacity >= size) {
memcpy(statesBuffer.data() + statesViews[id].first, &store[position + sizeof(DataProcessingStates::CommandHeader)], size);
statesViews[id].size = size;
updated[id] = true;
update.timestamp = timestamp;
update.generation = generation;
pushedMetricsLapse++;
} else if (statesViews[id].capacity < size) {
// Otherwise we need to reallocate.
int newCapacity = std::max(size, 64);
int first = statesBuffer.size();
statesBuffer.resize(statesBuffer.size() + newCapacity);
assert(first < statesBuffer.size());
memcpy(statesBuffer.data() + first, &store[position + sizeof(DataProcessingStates::CommandHeader)], size);
statesViews[id].first = first;
statesViews[id].size = size;
statesViews[id].capacity = newCapacity;
updated[id] = true;
update.timestamp = timestamp;
update.generation = generation;
pushedMetricsLapse++;
}
}
position += sizeof(DataProcessingStates::CommandHeader) + header.size;
}
assert(position == DataProcessingStates::STATES_BUFFER_SIZE);
// We reset the queue. Once again, the queue is filled in reverse order.
nextState.store(STATES_BUFFER_SIZE, std::memory_order_relaxed);
lastInsertedState.store(STATES_BUFFER_SIZE, std::memory_order_relaxed);
generation++;
}
void DataProcessingStates::updateState(CommandSpec cmd)
{
LOGP(debug, "Updating state {} with {}", cmd.id, std::string_view(cmd.data, cmd.size));
if (stateSpecs[cmd.id].name.empty()) {
throw runtime_error_f("StateID %d was not registered", (int)cmd.id);
}
if (cmd.id >= stateNames.size()) {
throw runtime_error_f("StateID %d is out of range", (int)cmd.id);
}
if (cmd.id >= updateInfos.size()) {
throw runtime_error_f("MetricID %d is out of range", (int)cmd.id);
}
// Do not update currently disabled states
if (enabled[cmd.id] == false) {
return;
}
pendingStates++;
// Add a static mutex to protect the queue
// Get the next available operation in an atomic way.
int size = sizeof(CommandHeader) + cmd.size;
if (size > 16384) {
throw runtime_error_f("State size is %d for state %s. States larger than 16384 bytes not supported for now.",
size, stateSpecs[cmd.id].name.c_str());
}
int idx = nextState.fetch_sub(size, std::memory_order_relaxed);
if (idx - size < 0) {
// We abort this command
pendingStates--;
while (pendingStates.load(std::memory_order_relaxed) > 0) {
// We need to wait for all the pending commands to be processed.
// This is needed because we are going to flush the queue.
// We cannot flush the queue while there are pending commands
// as we might end up with a command being processed twice.
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
processCommandQueue();
lastInsertedState.store(STATES_BUFFER_SIZE, std::memory_order_relaxed);
nextState.store(STATES_BUFFER_SIZE, std::memory_order_relaxed);
pendingStates++;
idx = nextState.fetch_sub(size, std::memory_order_relaxed);
} else if (idx < 0) {
while (idx < 0) {
// We need to wait for the flushing of the queue
std::this_thread::sleep_for(std::chrono::milliseconds(1));
idx = nextState.load(std::memory_order_relaxed);
}
return updateState(cmd);
}
// Save the state in the queue
assert(idx >= 0);
assert(cmd.id < statesViews.size());
int64_t timestamp = getTimestamp(realTimeBase, initialTimeOffset);
// We also write starting from idx - size, because we know this is
// reserved for us.
idx -= size;
CommandHeader header{(short)cmd.id, cmd.size, timestamp};
assert(idx >= 0);
assert(idx + sizeof(CommandHeader) + cmd.size <= store.size());
memcpy(&store.data()[idx], &header, sizeof(CommandHeader));
memcpy(&store.data()[idx + sizeof(CommandHeader)], cmd.data, cmd.size);
lastInsertedState = idx;
pendingStates--;
// Keep track of the number of commands we have received.
updatedMetricsLapse++;
}
void DataProcessingStates::flushChangedStates(std::function<void(std::string const&, int64_t, std::string_view)> const& callback)
{
publishingInvokedTotal++;
bool publish = false;
auto currentTimestamp = getTimestamp(realTimeBase, initialTimeOffset);
for (auto mi : registeredStates) {
auto& update = updateInfos[mi];
auto& spec = stateSpecs[mi];
auto& view = statesViews[mi];
if (updated[mi] == false && (currentTimestamp - update.timestamp) > spec.maxRefreshLatency) {
updated[mi] = true;
update.timestamp = currentTimestamp;
}
if (updated[mi] == false) {
continue;
}
int64_t delayPublished = (currentTimestamp - update.lastPublished);
assert(delayPublished >= 0);
if (delayPublished < spec.minPublishInterval) {
continue;
}
publish = true;
assert(view.first + view.size <= statesBuffer.size());
assert(view.first <= statesBuffer.size());
auto msg = std::string_view(statesBuffer.data() + view.first, view.size);
callback(spec.name.data(), update.timestamp, msg);
publishedMetricsLapse++;
update.lastPublished = currentTimestamp;
updated[mi] = false;
}
if (publish) {
publishingDoneTotal++;
}
static int64_t startTime = uv_hrtime();
int64_t now = uv_hrtime();
double averageInvocations = (publishingInvokedTotal * 1000000000) / (now - startTime);
double averagePublishing = (publishedMetricsLapse * 1000000000) / (now - startTime);
LOGP(debug, "Publishing invoked {} times / s, {} metrics published / s", (int)averageInvocations, (int)averagePublishing);
}
void DataProcessingStates::repack()
{
// Everytime we publish, we repack the states buffer so that we can minimize the
// amount of memory me use.
std::array<int, MAX_STATES> order;
std::iota(order.begin(), order.end(), 0);
std::stable_sort(order.begin(), order.begin() + statesViews.size(), [&](int a, int b) {
return statesViews[a].first < statesViews[b].first;
});
int position = 0;
for (size_t i = 0; i < order.size(); ++i) {
auto& view = statesViews[order[i]];
// If we have no size, we do not need to move anything.
if (view.size == 0) {
continue;
}
// If we are already in the correct place, do nothing.
if (view.first == position) {
continue;
}
memcpy(statesBuffer.data() + position, statesBuffer.data() + view.first, view.size);
view.first = position;
view.capacity = view.size;
position += view.size;
}
}
void DataProcessingStates::registerState(StateSpec const& spec)
{
if (stateSpecs[spec.stateId].name.size() != 0 && spec.name != stateSpecs[spec.stateId].name) {
auto currentName = stateSpecs[spec.stateId].name;
throw runtime_error_f("Metric %d already registered with name %s", spec.stateId, currentName.data(), spec.name.data());
}
auto currentMetric = std::find_if(stateSpecs.begin(), stateSpecs.end(), [&spec](StateSpec const& s) { return s.name == spec.name && s.stateId != spec.stateId; });
if (currentMetric != stateSpecs.end()) {
throw runtime_error_f("Metric %s already registered with id %d. Cannot reregister with %d.", spec.name.data(), currentMetric->stateId, spec.stateId);
}
stateSpecs[spec.stateId] = spec;
stateNames[spec.stateId] = spec.name;
int64_t currentTime = getTimestamp(realTimeBase, initialTimeOffset);
updateInfos[spec.stateId] = UpdateInfo{currentTime, currentTime};
updated[spec.stateId] = spec.sendInitialValue;
enabled[spec.stateId] = spec.defaultEnabled;
registeredStates.push_back(spec.stateId);
}
} // namespace o2::framework