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