artdaq  v3_09_06a
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_DEBUG(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_DEBUG(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  std::cerr << "Exception encountered starting Routing Manager thread: " << boost::diagnostic_information(e) << ", errno=" << errno << std::endl;
146  exit(3);
147  }
148  TLOG(TLVL_INFO) << "Started Routing Manager thread";
149 
150  return external_request_status_;
151 }
152 
154 {
155  report_string_ = "";
156  external_request_status_ = routing_manager_ptr_->shutdown(timeout);
157  if (!external_request_status_)
158  {
159  report_string_ = "Error shutting down ";
160  report_string_.append(app_name + ".");
161  }
162  return external_request_status_;
163 }
164 
165 bool artdaq::RoutingManagerApp::do_soft_initialize(fhicl::ParameterSet const& pset, uint64_t timeout, uint64_t timestamp)
166 {
167  report_string_ = "";
168  external_request_status_ = routing_manager_ptr_->soft_initialize(pset, timeout, timestamp);
169  if (!external_request_status_)
170  {
171  report_string_ = "Error soft-initializing ";
172  report_string_.append(app_name + " ");
173  report_string_.append("with ParameterSet = \"" + pset.to_string() + "\".");
174  }
175  return external_request_status_;
176 }
177 
178 bool artdaq::RoutingManagerApp::do_reinitialize(fhicl::ParameterSet const& pset, uint64_t timeout, uint64_t timestamp)
179 {
180  external_request_status_ = routing_manager_ptr_->reinitialize(pset, timeout, timestamp);
181  if (!external_request_status_)
182  {
183  report_string_ = "Error reinitializing ";
184  report_string_.append(app_name + " ");
185  report_string_.append("with ParameterSet = \"" + pset.to_string() + "\".");
186  }
187  return external_request_status_;
188 }
189 
191 {
192  TLOG_DEBUG(app_name + "App") << "Booted state entry action called.";
193 
194  // the destruction of any existing RoutingManagerCore has to happen in the
195  // Booted Entry action rather than the Initialized Exit action because the
196  // Initialized Exit action is only called after the "init" transition guard
197  // condition is executed.
198  routing_manager_ptr_.reset(nullptr);
199 }
200 
201 std::string artdaq::RoutingManagerApp::report(std::string const& which) const
202 {
203  std::string resultString;
204 
205  // if all that is requested is the latest state change result, return it
206  if (which == "transition_status")
207  {
208  if (report_string_.length() > 0) { return report_string_; }
209 
210  return "Success";
211  }
212 
215  //if (report_string_.length() > 0) {
216  // resultString.append("*** Overall status message:\r\n");
217  // resultString.append(report_string_ + "\r\n");
218  // resultString.append("*** Requested report response:\r\n");
219  //}
220 
221  // pass the request to the RoutingManagerCore instance, if it's available
222  if (routing_manager_ptr_ != nullptr)
223  {
224  resultString.append(routing_manager_ptr_->report(which));
225  }
226  else
227  {
228  resultString.append("This RoutingManager has not yet been initialized and ");
229  resultString.append("therefore can not provide reporting.");
230  }
231 
232  return resultString;
233 }
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.