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