artdaq  v3_06_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/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/Generators/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 lifetime issues
97  // associated with async threads and std::string::c_str().
98  fhicl::ParameterSet event_builder_pset = pset.get<fhicl::ParameterSet>("event_builder");
99  fhicl::ParameterSet art_pset = pset.get<fhicl::ParameterSet>("art", pset);
100 
101  artdaq::SharedMemoryEventManager event_manager(event_builder_pset, art_pset);
103 
104  int events_to_generate = pset.get<int>("events_to_generate", 0);
105  int event_count = 0;
106  artdaq::Fragment::sequence_id_t previous_sequence_id = -1;
107 
108  if (commandable_gen) {
109  commandable_gen->StartCmd(run, timeout, timestamp);
110  }
111 
112  TLOG(50) << "driver main before event_manager.startRun";
113  event_manager.startRun(run);
114 
115  // Read or generate fragments as rapidly as possible, and feed them
116  // into the Eventevent_manager. The throughput resulting from this design
117  // choice is likely to have the fragment reading (or generation)
118  // speed as the limiting factor
119  while ((commandable_gen && commandable_gen->getNext(frags)) ||
120  (gen && gen->getNext(frags))) {
121  TLOG(50) << "driver main: getNext returned frags.size()=" << frags.size() << " current event_count=" << event_count;
122  for (auto & val : frags) {
123  if (val->sequenceID() != previous_sequence_id) {
124  ++event_count;
125  previous_sequence_id = val->sequenceID();
126  }
127  if (events_to_generate != 0 && event_count > events_to_generate) {
128  if (commandable_gen) {
129  commandable_gen->StopCmd(timeout, timestamp);
130  }
131  break;
132  }
133 
134  auto start_time = std::chrono::steady_clock::now();
135  bool sts = false;
136  auto loop_count = 0;
137  while (!sts)
138  {
139  artdaq::FragmentPtr tempFrag;
140  sts = event_manager.AddFragment(std::move(val), 1000000, tempFrag);
141  if (!sts && event_count <= 10 && loop_count > 100)
142  {
143  TLOG(TLVL_ERROR) << "Fragment was not added after " << artdaq::TimeUtils::GetElapsedTime(start_time) << " s. Check art thread status!";
144  event_manager.endOfData();
145  exit(1);
146  }
147  val = std::move(tempFrag);
148  if (!sts)
149  {
150  loop_count++;
151  //usleep(10000);
152  }
153  }
154  }
155  frags.clear();
156 
157  if (events_to_generate != 0 && event_count >= events_to_generate) {
158  if (commandable_gen) {
159  commandable_gen->StopCmd(timeout, timestamp);
160  }
161  break;
162  }
163  }
164 
165  if (commandable_gen) {
166  commandable_gen->joinThreads();
167  }
168 
169  TLOG(TLVL_INFO) << "Fragments generated, waiting for art to process them.";
170  auto art_wait_start_time = std::chrono::steady_clock::now();
171  auto last_delta_time = std::chrono::steady_clock::now();
172  auto last_count = event_manager.size() - event_manager.WriteReadyCount(false);
173 
174  while (last_count > 0 && artdaq::TimeUtils::GetElapsedTime(last_delta_time) < 1.0)
175  {
176  auto this_count = event_manager.size() - event_manager.WriteReadyCount(false);
177  if (this_count != last_count) {
178  last_delta_time = std::chrono::steady_clock::now();
179  last_count = this_count;
180  }
181  usleep(1000);
182  }
183 
184  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).";
185  event_manager.endRun();
186  usleep(artdaq::TimeUtils::GetElapsedTimeMicroseconds(art_wait_start_time)); // Wait as long again for EndRun message to go through
187 
188  TLOG(TLVL_INFO) << "Shutting down art";
189  bool endSucceeded = false;
190  int attemptsToEnd = 1;
191  endSucceeded = event_manager.endOfData();
192  while (!endSucceeded && attemptsToEnd < 3) {
193  ++attemptsToEnd;
194  endSucceeded = event_manager.endOfData();
195  }
196  if (!endSucceeded) {
197  TLOG(TLVL_ERROR) << "Failed to shut down the reader and the SharedMemoryEventManager "
198  << "because the endOfData marker could not be pushed "
199  << "onto the queue.";
200  }
201 
202  metricMan->do_stop();
203 
205  return 0;
206 }
207 catch (std::string & x)
208 {
209  std::cerr << "Exception (type string) caught in artdaqDriver: " << x << '\n';
210  return 1;
211 }
212 catch (char const * m)
213 {
214  std::cerr << "Exception (type char const*) caught in artdaqDriver: ";
215  if (m)
216  {
217  std::cerr << m;
218  }
219  else
220  {
221  std::cerr << "[the value was a null pointer, so no message is available]";
222  }
223  std::cerr << '\n';
224 }
225 catch (...) {
226  artdaq::ExceptionHandler(artdaq::ExceptionHandlerRethrow::no,
227  "Exception caught in artdaqDriver");
228 }
229 
230 
231 template<typename B, typename D>
232 std::unique_ptr<D>
233 dynamic_unique_ptr_cast(std::unique_ptr<B>& p)
234 {
235  D* result = dynamic_cast<D*>(p.get());
236 
237  if (result) {
238  p.release();
239  return std::unique_ptr<D>(result);
240  }
241  return nullptr;
242 }
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.