artdaq  v3_12_02
RoutingManagerApp.cc
1 #include "TRACE/tracemf.h"
2 #include "artdaq/DAQdata/Globals.hh"
3 #define TRACE_NAME "RoutingManagerApp"
4 
5 #include "artdaq/Application/RoutingManagerApp.hh"
6 
7 #include <boost/bind.hpp>
8 #include <boost/exception/all.hpp>
9 #include <boost/lexical_cast.hpp>
10 #include <boost/thread.hpp>
11 
12 #include <memory>
13 
18 
19 // *******************************************************************
20 // *** The following methods implement the state machine operations.
21 // *******************************************************************
22 
23 bool artdaq::RoutingManagerApp::do_initialize(fhicl::ParameterSet const& pset, uint64_t timeout, uint64_t timestamp)
24 {
25  report_string_ = "";
27 
28  // in the following block, we first destroy the existing RoutingManager
29  // instance, then create a new one. Doing it in one step does not
30  // produce the desired result since that creates a new instance and
31  // then deletes the old one, and we need the opposite order.
32  routing_manager_ptr_.reset(nullptr);
33  routing_manager_ptr_ = std::make_unique<RoutingManagerCore>();
34  external_request_status_ = routing_manager_ptr_->initialize(pset, timeout, timestamp);
35  if (!external_request_status_)
36  {
37  report_string_ = "Error initializing ";
38  report_string_.append(app_name + " ");
39  report_string_.append("with ParameterSet = \"" + pset.to_string() + "\".");
40  }
41 
43 }
44 
45 bool artdaq::RoutingManagerApp::do_start(art::RunID id, uint64_t timeout, uint64_t timestamp)
46 {
47  report_string_ = "";
48  external_request_status_ = routing_manager_ptr_->start(id, timeout, timestamp);
49  if (!external_request_status_)
50  {
51  report_string_ = "Error starting ";
52  report_string_.append(app_name + " ");
53  report_string_.append("for run number ");
54  report_string_.append(boost::lexical_cast<std::string>(id.run()));
55  report_string_.append(", timeout ");
56  report_string_.append(boost::lexical_cast<std::string>(timeout));
57  report_string_.append(", timestamp ");
58  report_string_.append(boost::lexical_cast<std::string>(timestamp));
59  report_string_.append(".");
60  }
61 
62  boost::thread::attributes attrs;
63  attrs.set_stack_size(4096 * 2000); // 8 MB
64  try
65  {
66  routing_manager_thread_ = boost::thread(attrs, boost::bind(&RoutingManagerCore::process_event_table, routing_manager_ptr_.get()));
67  char tname[16]; // Size 16 - see man page pthread_setname_np(3) and/or prctl(2)
68  snprintf(tname, sizeof(tname) - 1, "%d-Routing", my_rank); // NOLINT
69  tname[sizeof(tname) - 1] = '\0'; // assure term. snprintf is not too evil :)
70  auto handle = routing_manager_thread_.native_handle();
71  pthread_setname_np(handle, tname);
72  }
73  catch (const boost::exception& e)
74  {
75  TLOG(TLVL_ERROR) << "Caught boost::exception starting RoutingManagerCore thread: " << boost::diagnostic_information(e) << ", errno=" << errno;
76  std::cerr << "Caught boost::exception starting RoutingManagerCore thread: " << boost::diagnostic_information(e) << ", errno=" << errno << std::endl;
77  exit(5);
78  }
79 
80  return external_request_status_;
81 }
82 
83 bool artdaq::RoutingManagerApp::do_stop(uint64_t timeout, uint64_t timestamp)
84 {
85  report_string_ = "";
86  external_request_status_ = routing_manager_ptr_->stop(timeout, timestamp);
87  if (!external_request_status_)
88  {
89  report_string_ = "Error stopping ";
90  report_string_.append(app_name + ".");
91  return false;
92  }
93 
94  if (routing_manager_thread_.joinable())
95  {
96  routing_manager_thread_.join();
97  }
98 
99  TLOG(TLVL_DEBUG + 32, app_name + "App") << "do_stop(uint64_t, uint64_t): "
100  << "Number of table entries sent = " << routing_manager_ptr_->get_update_count()
101  << ".";
102 
103  return external_request_status_;
104 }
105 
106 bool artdaq::RoutingManagerApp::do_pause(uint64_t timeout, uint64_t timestamp)
107 {
108  report_string_ = "";
109  external_request_status_ = routing_manager_ptr_->pause(timeout, timestamp);
110  if (!external_request_status_)
111  {
112  report_string_ = "Error pausing ";
113  report_string_.append(app_name + ".");
114  }
115  if (routing_manager_thread_.joinable())
116  {
117  routing_manager_thread_.join();
118  }
119 
120  TLOG(TLVL_DEBUG + 32, app_name + "App") << "do_pause(uint64_t, uint64_t): "
121  << "Number of table entries sent = " << routing_manager_ptr_->get_update_count()
122  << ".";
123 
124  return external_request_status_;
125 }
126 
127 bool artdaq::RoutingManagerApp::do_resume(uint64_t timeout, uint64_t timestamp)
128 {
129  report_string_ = "";
130  external_request_status_ = routing_manager_ptr_->resume(timeout, timestamp);
131  if (!external_request_status_)
132  {
133  report_string_ = "Error resuming ";
134  report_string_.append(app_name + ".");
135  }
136 
137  boost::thread::attributes attrs;
138  attrs.set_stack_size(4096 * 2000); // 8000 KB
139 
140  TLOG(TLVL_INFO) << "Starting Routing Manager thread";
141  try
142  {
143  routing_manager_thread_ = boost::thread(attrs, boost::bind(&RoutingManagerCore::process_event_table, routing_manager_ptr_.get()));
144  char tname[16]; // Size 16 - see man page pthread_setname_np(3) and/or prctl(2)
145  snprintf(tname, sizeof(tname) - 1, "%d-Routing", my_rank); // NOLINT
146  tname[sizeof(tname) - 1] = '\0'; // assure term. snprintf is not too evil :)
147  auto handle = routing_manager_thread_.native_handle();
148  pthread_setname_np(handle, tname);
149  }
150  catch (boost::exception const& e)
151  {
152  TLOG(TLVL_ERROR) << "Exception encountered starting Routing Manager thread: " << boost::diagnostic_information(e) << ", errno=" << errno;
153  std::cerr << "Exception encountered starting Routing Manager thread: " << boost::diagnostic_information(e) << ", errno=" << errno << std::endl;
154  exit(3);
155  }
156  TLOG(TLVL_INFO) << "Started Routing Manager thread";
157 
158  return external_request_status_;
159 }
160 
162 {
163  report_string_ = "";
164  external_request_status_ = routing_manager_ptr_->shutdown(timeout);
165  if (!external_request_status_)
166  {
167  report_string_ = "Error shutting down ";
168  report_string_.append(app_name + ".");
169  }
170  return external_request_status_;
171 }
172 
173 bool artdaq::RoutingManagerApp::do_soft_initialize(fhicl::ParameterSet const& pset, uint64_t timeout, uint64_t timestamp)
174 {
175  report_string_ = "";
176  external_request_status_ = routing_manager_ptr_->soft_initialize(pset, timeout, timestamp);
177  if (!external_request_status_)
178  {
179  report_string_ = "Error soft-initializing ";
180  report_string_.append(app_name + " ");
181  report_string_.append("with ParameterSet = \"" + pset.to_string() + "\".");
182  }
183  return external_request_status_;
184 }
185 
186 bool artdaq::RoutingManagerApp::do_reinitialize(fhicl::ParameterSet const& pset, uint64_t timeout, uint64_t timestamp)
187 {
188  external_request_status_ = routing_manager_ptr_->reinitialize(pset, timeout, timestamp);
189  if (!external_request_status_)
190  {
191  report_string_ = "Error reinitializing ";
192  report_string_.append(app_name + " ");
193  report_string_.append("with ParameterSet = \"" + pset.to_string() + "\".");
194  }
195  return external_request_status_;
196 }
197 
199 {
200  TLOG(TLVL_DEBUG + 32, app_name + "App") << "Booted state entry action called.";
201 
202  // the destruction of any existing RoutingManagerCore has to happen in the
203  // Booted Entry action rather than the Initialized Exit action because the
204  // Initialized Exit action is only called after the "init" transition guard
205  // condition is executed.
206  routing_manager_ptr_.reset(nullptr);
207 }
208 
209 std::string artdaq::RoutingManagerApp::report(std::string const& which) const
210 {
211  std::string resultString;
212 
213  // if all that is requested is the latest state change result, return it
214  if (which == "transition_status")
215  {
216  if (report_string_.length() > 0) { return report_string_; }
217 
218  return "Success";
219  }
220 
223  // if (report_string_.length() > 0) {
224  // resultString.append("*** Overall status message:\r\n");
225  // resultString.append(report_string_ + "\r\n");
226  // resultString.append("*** Requested report response:\r\n");
227  // }
228 
229  // pass the request to the RoutingManagerCore instance, if it's available
230  if (routing_manager_ptr_ != nullptr)
231  {
232  resultString.append(routing_manager_ptr_->report(which));
233  }
234  else
235  {
236  resultString.append("This RoutingManager has not yet been initialized and ");
237  resultString.append("therefore can not provide reporting.");
238  }
239 
240  return resultString;
241 }
bool do_resume(uint64_t timeout, uint64_t timestamp) override
Resume the RoutingManagerCore.
bool do_shutdown(uint64_t timeout) override
Shutdown the RoutingManagerCore.
bool do_stop(uint64_t timeout, uint64_t timestamp) override
Stop the RoutingManagerCore.
bool do_initialize(fhicl::ParameterSet const &pset, uint64_t timeout, uint64_t timestamp) override
Initialize the RoutingManagerCore.
bool external_request_status_
Whether the last command succeeded.
Definition: Commandable.hh:314
bool do_soft_initialize(fhicl::ParameterSet const &pset, uint64_t timeout, uint64_t timestamp) override
Soft-Initialize the RoutingManagerCore.
bool do_start(art::RunID id, uint64_t timeout, uint64_t timestamp) override
Start the RoutingManagerCore.
void BootedEnter() override
Action taken upon entering the &quot;Booted&quot; state.
std::string report(std::string const &) const override
If which is &quot;transition_status&quot;, report the status of the last transition. Otherwise pass through to ...
std::string report_string_
Status information about the last command.
Definition: Commandable.hh:315
bool do_pause(uint64_t timeout, uint64_t timestamp) override
Pause the RoutingManagerCore.
void process_event_table()
Main loop of the RoutingManagerCore. Determines when to send the next table update, asks the RoutingManagerPolicy for the table to send, and sends it.
RoutingManagerApp()
RoutingManagerApp Constructor.
bool do_reinitialize(fhicl::ParameterSet const &pset, uint64_t timeout, uint64_t timestamp) override
Reinitialize the RoutingManagerCore.