artdaq  v3_04_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  my_rank = 0;
77  // pull out the Metric part of the ParameterSet
78  fhicl::ParameterSet metric_pset;
79  try {
80  metric_pset = pset.get<fhicl::ParameterSet>("metrics");
81  }
82  catch (...) {} // OK if there's no metrics table defined in the FHiCL
83 
84  if (metric_pset.is_empty()) {
85  TLOG(TLVL_INFO) << "No metric plugins appear to be defined";
86  }
87  try {
88  metricMan->initialize(metric_pset, "artdaqDriver");
89  metricMan->do_start();
90  }
91  catch (...) {
92  }
93  artdaq::FragmentPtrs frags;
95  // Note: we are constrained to doing all this here rather than
96  // encapsulated neatly in a function due to the lieftime issues
97  // associated with async threads and std::string::c_str().
98  fhicl::ParameterSet event_builder_pset = pset.get<fhicl::ParameterSet>("event_builder");
99 
100  artdaq::SharedMemoryEventManager event_manager(event_builder_pset, pset);
102 
103  int events_to_generate = pset.get<int>("events_to_generate", 0);
104  int event_count = 0;
105  artdaq::Fragment::sequence_id_t previous_sequence_id = -1;
106 
107  if (commandable_gen) {
108  commandable_gen->StartCmd(run, timeout, timestamp);
109  }
110 
111  TLOG(50) << "driver main before event_manager.startRun";
112  event_manager.startRun(run);
113 
114  // Read or generate fragments as rapidly as possible, and feed them
115  // into the Eventevent_manager. The throughput resulting from this design
116  // choice is likely to have the fragment reading (or generation)
117  // speed as the limiting factor
118  while ((commandable_gen && commandable_gen->getNext(frags)) ||
119  (gen && gen->getNext(frags))) {
120  TLOG(50) << "driver main: getNext returned frags.size()=" << frags.size() << " current event_count=" << event_count;
121  for (auto & val : frags) {
122  if (val->sequenceID() != previous_sequence_id) {
123  ++event_count;
124  previous_sequence_id = val->sequenceID();
125  }
126  if (events_to_generate != 0 && event_count > events_to_generate) {
127  if (commandable_gen) {
128  commandable_gen->StopCmd(timeout, timestamp);
129  }
130  break;
131  }
132 
133  auto start_time = std::chrono::steady_clock::now();
134  bool sts = false;
135  auto loop_count = 0;
136  while (!sts)
137  {
138  artdaq::FragmentPtr tempFrag;
139  sts = event_manager.AddFragment(std::move(val), 1000000, tempFrag);
140  if (!sts && event_count <= 10 && loop_count > 100)
141  {
142  TLOG(TLVL_ERROR) << "Fragment was not added after " << artdaq::TimeUtils::GetElapsedTime(start_time) << " s. Check art thread status!";
143  event_manager.endOfData();
144  exit(1);
145  }
146  val = std::move(tempFrag);
147  if (!sts)
148  {
149  loop_count++;
150  //usleep(10000);
151  }
152  }
153  }
154  frags.clear();
155 
156  if (events_to_generate != 0 && event_count >= events_to_generate) {
157  if (commandable_gen) {
158  commandable_gen->StopCmd(timeout, timestamp);
159  }
160  break;
161  }
162  }
163 
164  if (commandable_gen) {
165  commandable_gen->joinThreads();
166  }
167 
168  TLOG(TLVL_INFO) << "Fragments generated, waiting for art to process them.";
169  auto art_wait_start_time = std::chrono::steady_clock::now();
170  auto last_delta_time = std::chrono::steady_clock::now();
171  auto last_count = event_manager.size() - event_manager.WriteReadyCount(false);
172 
173  while (last_count > 0 && artdaq::TimeUtils::GetElapsedTime(last_delta_time) < 1.0)
174  {
175  auto this_count = event_manager.size() - event_manager.WriteReadyCount(false);
176  if (this_count != last_count) {
177  last_delta_time = std::chrono::steady_clock::now();
178  last_count = this_count;
179  }
180  usleep(1000);
181  }
182 
183  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).";
184  event_manager.endRun();
185  usleep(artdaq::TimeUtils::GetElapsedTimeMicroseconds(art_wait_start_time)); // Wait as long again for EndRun message to go through
186 
187  TLOG(TLVL_INFO) << "Shutting down art";
188  bool endSucceeded = false;
189  int attemptsToEnd = 1;
190  endSucceeded = event_manager.endOfData();
191  while (!endSucceeded && attemptsToEnd < 3) {
192  ++attemptsToEnd;
193  endSucceeded = event_manager.endOfData();
194  }
195  if (!endSucceeded) {
196  TLOG(TLVL_ERROR) << "Failed to shut down the reader and the SharedMemoryEventManager "
197  << "because the endOfData marker could not be pushed "
198  << "onto the queue.";
199  }
200 
201  metricMan->do_stop();
202 
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...
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...
CommandableFragmentGenerator is a FragmentGenerator-derived abstract class that defines the interface...
void joinThreads()
Join any data-taking threads. Should be called when destructing CommandableFragmentGenerator.