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