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