-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoutput_device.cpp
290 lines (231 loc) · 9.38 KB
/
output_device.cpp
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
#include "chlog.hpp"
#include "ch_frb_io_internals.hpp"
using namespace std;
namespace ch_frb_io {
#if 0
}; // pacify emacs c-mode!
#endif
// Static factory function
shared_ptr<output_device> output_device::make(const output_device::initializer &ini_params)
{
output_device *p = new output_device(ini_params);
shared_ptr<output_device> ret(p);
ret->output_thread = std::thread(std::bind(&output_device::io_thread_main, ret));
return ret;
}
output_device::output_device(const output_device::initializer &ini_params_) :
ini_params(ini_params_)
{
// Note: no sanity-checking of ini_params needed here!
// XXX 32M is overkill! How much space should I use here?
this->_buffer = unique_ptr<uint8_t[]> (new uint8_t[32 * 1024 * 1024]);
}
void output_device::io_thread_main()
{
// Note: if 'io_thread_allowed_cores' is an empty list, no core-pinning will be done.
pin_thread_to_cores(ini_params.io_thread_allowed_cores);
// If io_thread_name is unspecified, assign a default thread name.
string thread_name = ini_params.io_thread_name;
if (thread_name.size() == 0)
thread_name = "io_thread[" + ini_params.device_name + "]";
chime_log_set_thread_name(thread_name);
if (ini_params.verbosity >= 2)
chlog("thread starting up");
for (;;) {
// Pull a write_chunk_request off the queue!
shared_ptr<write_chunk_request> w = this->pop_write_request();
if (!w)
break;
shared_ptr<assembled_chunk> chunk = w->chunk;
string error_message;
string link_src;
unique_lock<mutex> ulock(chunk->filename_mutex);
// If this write request is a duplicate of a previous write request, return immediately.
if (chunk->filename_set.count(w->filename) > 0) {
ulock.unlock();
if (ini_params.verbosity >= 3)
chlog("write request '" + w->filename + "' is a duplicate, skipping...");
w->status_changed(true, true, "SUCCEEDED", "Duplicate filename " + w->filename + " already written");
continue;
}
// If this write request is a "pseudo-duplicate", i.e. the same chunk has been
// previously written to a different filename on the same output_device, then
// we'll make a hard link instead of a copy.
auto p = chunk->filename_map.find(ini_params.device_name);
if (p != chunk->filename_map.end())
link_src = p->second;
ulock.unlock();
bool success = true;
try {
if (file_exists(w->filename))
throw runtime_error("Assembled chunk msgpack file to be written already exists: " + w->filename);
else if (link_src.size() == 0) {
//sleep(3);
chunk->write_msgpack_file(w->filename, false, this->_buffer.get()); // compress=false
} else {
if (ini_params.verbosity >= 3)
chlog("write request '" + w->filename + "' is a pseudo-duplicate, will hard-link from '" + link_src + "' instead of writing new copy");
hard_link(link_src, w->filename);
}
} catch (exception &e) {
// Note: we now include the exception text in the error_message.
success = false;
error_message = "Write msgpack file '" + w->filename + "' failed: " + e.what();
}
if (success) {
ulock.lock();
chunk->filename_set.insert(w->filename);
chunk->filename_map[ini_params.device_name] = w->filename;
ulock.unlock();
}
if (error_message.size() > 0 && ini_params.verbosity >= 1)
chlog(error_message);
else if (error_message.size() == 0 && ini_params.verbosity >= 3)
chlog("wrote " + w->filename);
w->status_changed(true, success, success ? "SUCCEEDED" : "FAILED", error_message);
}
if (ini_params.verbosity >= 2)
chlog("received NULL write_chunk_request; exiting.");
}
// Called by an "external" thread (assembler thread or RPC thread).
bool output_device::enqueue_write_request(const shared_ptr<write_chunk_request> &req)
{
if (!req)
throw runtime_error("ch_frb_io::output_device::enqueue_write_request(): req is null");
if (!req->chunk)
throw runtime_error("ch_frb_io::output_device::enqueue_write_request(): req->chunk is null");
if (req->filename.size() == 0)
throw runtime_error("ch_frb_io::output_device::enqueue_write_request(): req->filename is an empty string");
if (!is_prefix(this->ini_params.device_name, req->filename))
throw runtime_error("ch_frb_io::output_device::enqueue_write_request(): req->filename, device_name mismatch");
if (req->need_rfi_mask && (req->chunk->nrfifreq <= 0)) {
// I decided to throw an exception here, instead of returning false,
// so that we get a "verbose" error message instead of an undiagnosed failure.
// (The L1 server is responsible for not crashing, either by catching the exception
// or by checking this error condition in advance.)
throw runtime_error("ch_frb_io: enqueue_write_request() was called with need_rfi_mask=true,"
" but this server instance is not saving the RFI mask");
}
unique_lock<std::mutex> ulock(_lock);
if (end_stream_called) {
req->status_changed(true, false, "FAILED", "stream ending");
return false;
}
if (req->need_rfi_mask && !req->chunk->has_rfi_mask) {
_awaiting_rfi.push_back(req);
req->status_changed(false, true, "AWAITING_RFI", "Waiting for RFI mask to be computed");
} else {
_write_reqs.push(req);
req->status_changed(false, true, "QUEUED", "Queued for writing");
_cond.notify_all();
}
int n = _write_reqs.size();
int na = _awaiting_rfi.size();
ulock.unlock();
if (ini_params.verbosity >= 3) {
chlog("enqueued write request: filename " + req->filename
+ ", beam " + to_string(req->chunk->beam_id)
+ ", chunk " + to_string(req->chunk->ichunk)
+ ", FPGA counts " + to_string(req->chunk->fpga_begin)
+ ", write_queue_size=" + to_string(n)
+ ", awaiting_rfi_size=" + to_string(na));
}
return true;
}
int output_device::count_queued_write_requests() {
unique_lock<std::mutex> ulock(_lock);
int rtn = _write_reqs.size();
ulock.unlock();
return rtn;
}
// This gets called by the chime_mask_counter class in rf_pipelines
// to tell us that a chunk's RFI mask has been filled in.
void output_device::filled_rfi_mask(const std::shared_ptr<assembled_chunk> &chunk) {
// FIXME? -- simplest approach -- tell i/o thread(s) waiting on the
// queue not being empty that something may have happened!
unique_lock<std::mutex> ulock(_lock);
_cond.notify_all();
ulock.unlock();
}
// Called internally by I/O thread.
shared_ptr<write_chunk_request> output_device::pop_write_request()
{
unique_lock<std::mutex> ulock(_lock);
for (;;) {
// Check whether any chunks that were waiting for RFI masks to be
// filled in have been.
for (auto req=_awaiting_rfi.begin(); req!=_awaiting_rfi.end(); req++) {
if ((*req)->chunk->has_rfi_mask) {
cout << "Chunk " << (*req)->filename << " got its RFI mask!" << endl;
_write_reqs.push((*req));
(*req)->status_changed(false, true, "QUEUED", "RFI mask received; queued for writing");
auto toerase = req;
req--;
_awaiting_rfi.erase(toerase);
}
}
if (!_write_reqs.empty())
break;
if (end_stream_called && _awaiting_rfi.empty())
return shared_ptr<write_chunk_request> ();
_cond.wait(ulock);
}
shared_ptr<write_chunk_request> ret = _write_reqs.top();
_write_reqs.pop();
_cond.notify_all();
int n = _write_reqs.size();
ulock.unlock();
if (ini_params.verbosity >= 3) {
chlog("dequeued write request: filename " + ret->filename
+ ", beam " + to_string(ret->chunk->beam_id)
+ ", chunk " + to_string(ret->chunk->ichunk)
+ ", FPGA counts " + to_string(ret->chunk->fpga_begin)
+ ", write_queue_size=" + to_string(n));
}
return ret;
}
void output_device::end_stream(bool wait)
{
unique_lock<std::mutex> ulock(_lock);
// Note: multiple calls to end_stream() are OK.
this->end_stream_called = true;
// If 'wait' is false, cancel all pending writes and return.
if (!wait) {
while (!_write_reqs.empty())
_write_reqs.pop();
_awaiting_rfi.clear();
_cond.notify_all();
return;
}
// If 'wait' is true, wait for pending writes to complete before returning.
while (!_write_reqs.empty() || !_awaiting_rfi.empty())
_cond.wait(ulock);
}
void output_device::join_thread()
{
unique_lock<std::mutex> ulock(_lock);
if (!end_stream_called)
throw runtime_error("output_device::join_thread() called, with no preceding call to end_stream()");
// Multiple calls to join_thread() are OK, but if this is not the first
// time join_thread() has been called, we still need to check that the
// thread has actually joined, and wait for it if not.
if (join_thread_called) {
while (!thread_joined)
_cond.wait(ulock);
return;
}
// If we get here, this is the first time join_thread() has been called.
// Set the 'join_thread_called' flag and drop the lock...
join_thread_called = true;
_cond.notify_all();
ulock.unlock();
// Now join the thread. The sychronization logic above guarantees that no
// other threads will call output_thread.join(). (Instead, they will wait
// for this thread to set the 'thread_joined' flag.)
output_thread.join();
// Now re-acquire the lock and set the 'thread_joined' flag.
ulock.lock();
thread_joined = true;
_cond.notify_all();
}
} // namespace ch_frb_io