1 #include "artdaq/Application/BoardReaderApp.hh"
2 #include "artdaq-core/Utilities/ExceptionHandler.hh"
4 #include "artdaq/DAQdata/Globals.hh"
5 #define TRACE_NAME (app_name + "_BoardReaderApp").c_str() // NOLINT
11 : fragment_receiver_ptr_(nullptr)
22 external_request_status_ =
true;
28 TLOG(TLVL_DEBUG) <<
"Initializing first deleting old instance " <<
static_cast<void*
>(fragment_receiver_ptr_.get());
29 fragment_receiver_ptr_.reset(
nullptr);
30 fragment_receiver_ptr_ = std::make_unique<BoardReaderCore>(*this);
31 TLOG(TLVL_DEBUG) <<
"Initializing new BoardReaderCore at " <<
static_cast<void*
>(fragment_receiver_ptr_.get()) <<
" with pset " << pset.to_string();
32 external_request_status_ = fragment_receiver_ptr_->initialize(pset, timeout, timestamp);
33 if (!external_request_status_)
35 report_string_ =
"Error initializing ";
36 report_string_.append(app_name +
" ");
37 report_string_.append(
"with ParameterSet = \"" + pset.to_string() +
"\".");
40 TLOG(TLVL_DEBUG) <<
"do_initialize(fhicl::ParameterSet, uint64_t, uint64_t): "
41 <<
"Done initializing.";
42 return external_request_status_;
52 fragment_receiver_ptr_->SetStartTransitionTimeout(timeout);
53 external_request_status_ =
true;
55 boost::thread::attributes attrs;
56 attrs.set_stack_size(4096 * 2000);
61 snprintf(tname,
sizeof(tname)-1,
"%d-FragOutput", my_rank);
62 tname[
sizeof(tname)-1] =
'\0';
63 auto handle = fragment_output_thread_.native_handle();
64 pthread_setname_np(handle, tname);
66 snprintf(tname,
sizeof(tname)-1,
"%d-FragInput", my_rank);
67 tname[
sizeof(tname)-1] =
'\0';
68 handle = fragment_input_thread_.native_handle();
69 pthread_setname_np(handle, tname);
71 catch (
const boost::exception& e)
73 std::stringstream exception_string;
74 exception_string <<
"Caught boost::exception starting Fragment Processing threads: " << boost::diagnostic_information(e) <<
", errno=" << errno;
76 ExceptionHandler(ExceptionHandlerRethrow::yes, exception_string.str());
79 auto start_wait = std::chrono::steady_clock::now();
80 while (!fragment_receiver_ptr_->GetSenderThreadActive() || !fragment_receiver_ptr_->GetReceiverThreadActive())
82 if (TimeUtils::GetElapsedTime(start_wait) > timeout)
84 TLOG(TLVL_ERROR) <<
"Timeout occurred waiting for BoardReaderCore threads to start. Timeout = " << timeout <<
" s, Time waited = " << TimeUtils::GetElapsedTime(start_wait) <<
" s,"
85 <<
" Receiver ready: " << std::boolalpha << fragment_receiver_ptr_->GetReceiverThreadActive() <<
", Sender ready: " << fragment_receiver_ptr_->GetSenderThreadActive();
86 external_request_status_ =
false;
93 if (external_request_status_)
95 external_request_status_ = fragment_receiver_ptr_->start(
id, timeout, timestamp);
98 if (!external_request_status_)
100 report_string_ =
"Error starting ";
101 report_string_.append(app_name +
" ");
102 report_string_.append(
"for run number ");
103 report_string_.append(boost::lexical_cast<std::string>(
id.run()));
104 report_string_.append(
", timeout ");
105 report_string_.append(boost::lexical_cast<std::string>(timeout));
106 report_string_.append(
", timestamp ");
107 report_string_.append(boost::lexical_cast<std::string>(timestamp));
108 report_string_.append(
".");
111 return external_request_status_;
117 external_request_status_ = fragment_receiver_ptr_->stop(timeout, timestamp);
118 if (!external_request_status_)
120 report_string_ =
"Error stopping ";
121 report_string_.append(app_name +
".");
124 if (fragment_input_thread_.joinable())
126 TLOG(TLVL_DEBUG) <<
"Joining fragment input (Generator) thread";
127 fragment_input_thread_.join();
129 if (fragment_output_thread_.joinable())
131 TLOG(TLVL_DEBUG) <<
"Joining fragment output (Sender) thread";
132 fragment_output_thread_.join();
135 TLOG(TLVL_DEBUG) <<
"BoardReader Stopped. Getting run statistics";
136 int number_of_fragments_sent = -1;
137 if (fragment_receiver_ptr_)
139 number_of_fragments_sent = fragment_receiver_ptr_->GetFragmentsProcessed();
141 TLOG(TLVL_DEBUG) <<
"do_stop(uint64_t, uint64_t): "
142 <<
"Number of fragments sent = " << number_of_fragments_sent
145 return external_request_status_;
151 external_request_status_ = fragment_receiver_ptr_->pause(timeout, timestamp);
152 if (!external_request_status_)
154 report_string_ =
"Error pausing ";
155 report_string_.append(app_name +
".");
158 if (fragment_input_thread_.joinable()) fragment_input_thread_.join();
159 if (fragment_output_thread_.joinable()) fragment_output_thread_.join();
160 int number_of_fragments_sent = fragment_receiver_ptr_->GetFragmentsProcessed();
161 TLOG(TLVL_DEBUG) <<
"do_pause(uint64_t, uint64_t): "
162 <<
"Number of fragments sent = " << number_of_fragments_sent
165 return external_request_status_;
175 external_request_status_ =
true;
177 boost::thread::attributes attrs;
178 attrs.set_stack_size(4096 * 2000);
183 snprintf(tname,
sizeof(tname)-1,
"%d-FragOutput", my_rank);
184 tname[
sizeof(tname)-1] =
'\0';
185 auto handle = fragment_output_thread_.native_handle();
186 pthread_setname_np(handle, tname);
189 snprintf(tname,
sizeof(tname)-1,
"%d-FragInput", my_rank);
190 tname[
sizeof(tname)-1] =
'\0';
191 handle = fragment_input_thread_.native_handle();
192 pthread_setname_np(handle, tname);
194 catch (
const boost::exception& e)
196 std::stringstream exception_string;
197 exception_string <<
"Caught boost::exception starting Fragment Processing threads: " << boost::diagnostic_information(e) <<
", errno=" << errno;
199 ExceptionHandler(ExceptionHandlerRethrow::yes, exception_string.str());
202 auto start_wait = std::chrono::steady_clock::now();
203 while (!fragment_receiver_ptr_->GetSenderThreadActive() || !fragment_receiver_ptr_->GetReceiverThreadActive())
205 if (TimeUtils::GetElapsedTimeMicroseconds(start_wait) > timeout * 1000000)
207 TLOG(TLVL_ERROR) <<
"Timeout occurred waiting for BoardReaderCore threads to start. Timeout = " << timeout <<
" s, Time waited = " << TimeUtils::GetElapsedTime(start_wait) <<
" s,"
208 <<
" Receiver ready: " << std::boolalpha << fragment_receiver_ptr_->GetReceiverThreadActive() <<
", Sender ready: " << fragment_receiver_ptr_->GetSenderThreadActive();
209 external_request_status_ =
false;
216 if (external_request_status_)
218 external_request_status_ = fragment_receiver_ptr_->resume(timeout, timestamp);
220 if (!external_request_status_)
222 report_string_ =
"Error resuming ";
223 report_string_.append(app_name +
".");
225 return external_request_status_;
231 external_request_status_ = fragment_receiver_ptr_->shutdown(timeout);
234 if (fragment_input_thread_.joinable()) fragment_input_thread_.join();
235 if (fragment_output_thread_.joinable()) fragment_output_thread_.join();
236 if (!external_request_status_)
238 report_string_ =
"Error shutting down ";
239 report_string_.append(app_name +
".");
241 return external_request_status_;
247 external_request_status_ = fragment_receiver_ptr_->soft_initialize(pset, timeout, timestamp);
248 if (!external_request_status_)
250 report_string_ =
"Error soft-initializing ";
251 report_string_.append(app_name +
" ");
252 report_string_.append(
"with ParameterSet = \"" + pset.to_string() +
"\".");
254 return external_request_status_;
259 external_request_status_ = fragment_receiver_ptr_->reinitialize(pset, timeout, timestamp);
260 if (!external_request_status_)
262 report_string_ =
"Error reinitializing ";
263 report_string_.append(app_name +
" ");
264 report_string_.append(
"with ParameterSet = \"" + pset.to_string() +
"\".");
266 return external_request_status_;
271 TLOG(TLVL_DEBUG) <<
"Booted state entry action called.";
277 fragment_receiver_ptr_.reset(
nullptr);
282 external_request_status_ = fragment_receiver_ptr_->metaCommand(command, arg);
283 if (!external_request_status_)
285 report_string_ =
"Error running meta-command on ";
286 report_string_.append(app_name +
" ");
287 report_string_.append(
"with command = \"" + command +
"\", arg = \"" + arg +
"\".");
289 return external_request_status_;
294 std::string resultString;
297 if (which ==
"transition_status")
299 if (report_string_.length() > 0) {
return report_string_; }
313 if (fragment_receiver_ptr_ !=
nullptr)
315 resultString.append(fragment_receiver_ptr_->report(which));
319 resultString.append(
"This BoardReader has not yet been initialized and ");
320 resultString.append(
"therefore can not provide reporting.");
bool do_soft_initialize(fhicl::ParameterSet const &pset, uint64_t timeout, uint64_t timestamp) override
Soft-Initialize the BoardReaderCore.
bool do_resume(uint64_t timeout, uint64_t timestamp) override
Resume the BoardReaderCore.
bool do_stop(uint64_t timeout, uint64_t timestamp) override
Stop the BoardReaderCore.
bool do_meta_command(std::string const &command, std::string const &arg) override
Perform a user-defined command (passed to CommandableFragmentGenerator)
bool do_shutdown(uint64_t timeout) override
Shutdown the BoardReaderCore.
bool do_reinitialize(fhicl::ParameterSet const &pset, uint64_t timeout, uint64_t timestamp) override
Reinitialize the BoardReaderCore.
std::string report(std::string const &which) const override
If which is "transition_status", report the status of the last transition. Otherwise pass through to ...
void send_fragments()
Main working loop of the BoardReaderCore, pt. 2.
BoardReaderApp()
BoardReaderApp Constructor.
void BootedEnter() override
Action taken upon entering the "Booted" state.
void receive_fragments()
Main working loop of the BoardReaderCore.
bool do_start(art::RunID id, uint64_t timeout, uint64_t timestamp) override
Start the BoardReaderCore.
bool do_initialize(fhicl::ParameterSet const &pset, uint64_t timeout, uint64_t timestamp) override
Initialize the BoardReaderCore.
bool do_pause(uint64_t timeout, uint64_t timestamp) override
Pause the BoardReaderCore.