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