artdaq  v3_09_06
driver.cc
1 //
2 // artdaqDriver is a program for testing the behavior of the generic
3 // RawInput source. Run 'artdaqDriver --help' to get a description of the
4 // expected command-line parameters.
5 //
6 //
7 // The current version generates simple data fragments, for testing
8 // that data are transmitted without corruption from the
9 // artdaq::Eventevent_manager through to the artdaq::RawInput source.
10 //
11 #define TRACE_NAME "artdaqDriver"
12 
13 #include "art/Framework/Art/artapp.h"
14 #include "artdaq-core/Data/Fragment.hh"
15 #include "artdaq-core/Generators/FragmentGenerator.hh"
16 #include "artdaq-core/Utilities/ExceptionHandler.hh"
17 #include "artdaq/DAQdata/GenericFragmentSimulator.hh"
18 
19 #include <boost/program_options.hpp>
20 #include "artdaq-core/Generators/makeFragmentGenerator.hh"
21 #include "artdaq-utilities/Plugins/MetricManager.hh"
22 #include "artdaq/DAQdata/Globals.hh"
23 #include "artdaq/Generators/makeCommandableFragmentGenerator.hh"
24 #include "cetlib/filepath_maker.h"
25 #include "fhiclcpp/ParameterSet.h"
26 #include "fhiclcpp/make_ParameterSet.h"
27 
28 #include <csignal>
29 #include <iostream>
30 #include <memory>
31 #include <utility>
32 #include "artdaq/Application/LoadParameterSet.hh"
33 #include "artdaq/ArtModules/detail/ArtConfig.hh"
34 #include "artdaq/DAQrate/SharedMemoryEventManager.hh"
35 
36 volatile int events_to_generate;
37 void sig_handler(int /*unused*/) { events_to_generate = -1; }
38 
39 template<typename B, typename D>
40 std::unique_ptr<D>
41 dynamic_unique_ptr_cast(std::unique_ptr<B>& p);
42 
43 int main(int argc, char* argv[]) try
44 {
45  struct Config
46  {
47  fhicl::Atom<int> run_number{fhicl::Name{"run_number"}, fhicl::Comment{"Run number to use for output file"}, 1};
48  fhicl::Atom<bool> debug_cout{fhicl::Name{"debug_cout"}, fhicl::Comment{"Whether to print debug messages to console"}, false};
49  fhicl::Atom<uint64_t> transition_timeout{fhicl::Name{"transition_timeout"}, fhicl::Comment{"Timeout to use (in seconds) for automatic transitions"}, 30};
50  fhicl::Table<artdaq::CommandableFragmentGenerator::Config> generator{fhicl::Name{"fragment_receiver"}};
51  fhicl::Table<artdaq::MetricManager::Config> metrics{fhicl::Name{"metrics"}};
52  fhicl::Table<artdaq::SharedMemoryEventManager::Config> event_builder{fhicl::Name{"event_builder"}};
53  fhicl::Atom<int> events_to_generate{fhicl::Name{"events_to_generate"}, fhicl::Comment{"Number of events to generate and process"}, 0};
54  fhicl::TableFragment<art::Config> art_config;
55  };
56  auto pset = LoadParameterSet<Config>(argc, argv, "driver", "The artdaqDriver executable runs a Fragment Generator and an art process, acting as a \"unit integration\" test for a data source");
57 
58  int run = pset.get<int>("run_number", 1);
59  bool debug = pset.get<bool>("debug_cout", false);
60  auto timeout = pset.get<uint64_t>("transition_timeout", 30);
61  uint64_t timestamp = 0;
62 
63  artdaq::configureMessageFacility("artdaqDriver", true, debug);
64 
65  auto fragment_receiver_pset = pset.get<fhicl::ParameterSet>("fragment_receiver");
66 
67  std::unique_ptr<artdaq::FragmentGenerator>
68  gen(artdaq::makeFragmentGenerator(fragment_receiver_pset.get<std::string>("generator"),
69  fragment_receiver_pset));
70 
71  std::unique_ptr<artdaq::CommandableFragmentGenerator> commandable_gen =
72  dynamic_unique_ptr_cast<artdaq::FragmentGenerator, artdaq::CommandableFragmentGenerator>(gen);
73 
74  my_rank = 0;
75  // pull out the Metric part of the ParameterSet
76  fhicl::ParameterSet metric_pset;
77  try
78  {
79  metric_pset = pset.get<fhicl::ParameterSet>("metrics");
80  }
81  catch (...)
82  {} // OK if there's no metrics table defined in the FHiCL
83 
84  if (metric_pset.is_empty())
85  {
86  TLOG(TLVL_INFO) << "No metric plugins appear to be defined";
87  }
88  try
89  {
90  metricMan->initialize(metric_pset, "artdaqDriver");
91  metricMan->do_start();
92  }
93  catch (...)
94  {
95  }
96  artdaq::FragmentPtrs frags;
98  // Note: we are constrained to doing all this here rather than
99  // encapsulated neatly in a function due to the lifetime issues
100  // associated with async threads and std::string::c_str().
101  auto event_builder_pset = pset.get<fhicl::ParameterSet>("event_builder");
102  auto art_pset = pset.get<fhicl::ParameterSet>("art", pset);
103 
104  artdaq::SharedMemoryEventManager event_manager(event_builder_pset, art_pset);
106 
107  int events_to_generate = pset.get<int>("events_to_generate", 0);
108  int event_count = 0;
109  artdaq::Fragment::sequence_id_t previous_sequence_id = -1;
110 
111  if (commandable_gen)
112  {
113  commandable_gen->StartCmd(run, timeout, timestamp);
114  }
115 
116  TLOG(50) << "driver main before event_manager.startRun";
117  event_manager.startRun(run);
118 
119  // Read or generate fragments as rapidly as possible, and feed them
120  // into the Eventevent_manager. The throughput resulting from this design
121  // choice is likely to have the fragment reading (or generation)
122  // speed as the limiting factor
123  while ((commandable_gen && commandable_gen->getNext(frags)) ||
124  (gen && gen->getNext(frags)))
125  {
126  TLOG(50) << "driver main: getNext returned frags.size()=" << frags.size() << " current event_count=" << event_count;
127  for (auto& val : frags)
128  {
129  if (val->sequenceID() != previous_sequence_id)
130  {
131  ++event_count;
132  previous_sequence_id = val->sequenceID();
133  }
134  if (events_to_generate != 0 && event_count > events_to_generate)
135  {
136  if (commandable_gen)
137  {
138  commandable_gen->StopCmd(timeout, timestamp);
139  }
140  break;
141  }
142 
143  auto start_time = std::chrono::steady_clock::now();
144  bool sts = false;
145  auto loop_count = 0;
146  while (!sts)
147  {
148  artdaq::FragmentPtr tempFrag;
149  sts = event_manager.AddFragment(std::move(val), 1000000, tempFrag);
150  if (!sts && event_count <= 10 && loop_count > 100)
151  {
152  TLOG(TLVL_ERROR) << "Fragment was not added after " << artdaq::TimeUtils::GetElapsedTime(start_time) << " s. Check art thread status!";
153  event_manager.endOfData();
154  exit(1);
155  }
156  val = std::move(tempFrag);
157  if (!sts)
158  {
159  loop_count++;
160  //usleep(10000);
161  }
162  }
163  }
164  frags.clear();
165 
166  if (events_to_generate != 0 && event_count >= events_to_generate)
167  {
168  if (commandable_gen)
169  {
170  commandable_gen->StopCmd(timeout, timestamp);
171  }
172  break;
173  }
174  }
175 
176  if (commandable_gen)
177  {
178  commandable_gen->joinThreads();
179  }
180 
181 #if 0
182  volatile bool keep_looping = true;
183  while (keep_looping)
184  {
185  usleep(10000);
186  }
187 #endif
188 
189  TLOG(TLVL_INFO) << "Fragments generated, waiting for art to process them.";
190  auto art_wait_start_time = std::chrono::steady_clock::now();
191  auto last_delta_time = std::chrono::steady_clock::now();
192  auto last_count = event_manager.size() - event_manager.WriteReadyCount(false);
193 
194  while (last_count > 0 && artdaq::TimeUtils::GetElapsedTime(last_delta_time) < 1.0)
195  {
196  auto this_count = event_manager.size() - event_manager.WriteReadyCount(false);
197  if (this_count != last_count)
198  {
199  last_delta_time = std::chrono::steady_clock::now();
200  last_count = this_count;
201  }
202  usleep(1000);
203  }
204 
205  TLOG(TLVL_INFO) << "Ending Run, waited " << std::setprecision(2) << artdaq::TimeUtils::GetElapsedTime(art_wait_start_time) << " seconds for art to process events. (" << last_count << " buffers remain).";
206  event_manager.endRun();
207  usleep(artdaq::TimeUtils::GetElapsedTimeMicroseconds(art_wait_start_time)); // Wait as long again for EndRun message to go through
208 
209  TLOG(TLVL_INFO) << "Shutting down art";
210  bool endSucceeded = false;
211  int attemptsToEnd = 1;
212  endSucceeded = event_manager.endOfData();
213  while (!endSucceeded && attemptsToEnd < 3)
214  {
215  ++attemptsToEnd;
216  endSucceeded = event_manager.endOfData();
217  }
218  if (!endSucceeded)
219  {
220  TLOG(TLVL_ERROR) << "Failed to shut down the reader and the SharedMemoryEventManager "
221  << "because the endOfData marker could not be pushed "
222  << "onto the queue.";
223  }
224 
225  metricMan->do_stop();
226 
228  return 0;
229 }
230 catch (std::string& x)
231 {
232  std::cerr << "Exception (type string) caught in artdaqDriver: " << x << '\n';
233  return 1;
234 }
235 catch (char const* m)
236 {
237  std::cerr << "Exception (type char const*) caught in artdaqDriver: ";
238  if (m != nullptr)
239  {
240  std::cerr << m;
241  }
242  else
243  {
244  std::cerr << "[the value was a null pointer, so no message is available]";
245  }
246  std::cerr << '\n';
247 }
248 catch (...)
249 {
250  artdaq::ExceptionHandler(artdaq::ExceptionHandlerRethrow::no,
251  "Exception caught in artdaqDriver");
252  exit(-2);
253 }
254 
255 template<typename B, typename D>
256 std::unique_ptr<D>
257 dynamic_unique_ptr_cast(std::unique_ptr<B>& p)
258 {
259  D* result = dynamic_cast<D*>(p.release());
260 
261  if (result)
262  {
263  return std::unique_ptr<D>(result);
264  }
265  return nullptr;
266 }
The SharedMemoryEventManager is a SharedMemoryManger which tracks events as they are built...
static void CleanUpGlobals()
Clean up statically-allocated Manager class instances.
Definition: Globals.hh:150
void StopCmd(uint64_t timeout, uint64_t timestamp)
Stop the CommandableFragmentGenerator.
void StartCmd(int run, uint64_t timeout, uint64_t timestamp)
Start the CommandableFragmentGenerator.
bool getNext(FragmentPtrs &output) overridefinal
getNext calls either applyRequests or getNext_ to get any data that is ready to be sent to the EventB...
Configuration for simple_metric_sender.
CommandableFragmentGenerator is a FragmentGenerator-derived abstract class that defines the interface...
void joinThreads()
Join any data-taking threads. Should be called when destructing CommandableFragmentGenerator.