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